There is a deque of size n and you want to use it now, but unfortunately it's not empty yet. So, before using it, you're forced to do this boring work: clear it.
Each time you pop the i th number from the left side, you'll get L * wi unhappiness points. Similarly, you'll get R * wi unhappiness points if you take the i th number from the right side.
However, keep taking numbers from one side will make the work more boring. So if the previous operation is the same, you'll get qL or qR extra unhappiness points, depending on the side you take numbers from. L , R , qL and qR are all constant. Now given L , R , qL , qR and the deque, you want to know the minimum unhappiness you can get.
Input
There are multiple test cases. For each test case: the first line contains 5 integers, n , L , R , qL and qR , seperated by a single space. the second line contains n integers, the i th integer is wi (1 ≤ n≤ 100000, 1 ≤ n , L , R , qL , qR, wi ≤ 100)
Output
For each test case, output an integer in a line, indicating your answer.
Sample Input
2 3 4 5 6
1 2
5 1 10 1 100
1 2 3 4 5
Sample Output
11
19
根据题意,左右两边可出每次出去都是ql|qr*wi,第二次和上一次出的方向相同的话需要+qr|ql
先求一下前缀和用于方便求n-i项和,
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll inf=1e18;
const int maxn=1e5+100;
int T;
int n,m,l,r,ql,qr;
int sum[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>l>>r>>ql>>qr)
{
memset(sum,0,sizeof sum);
for(int i=1; i<=n; i++)
{
cin>>sum[i];
sum[i]+=sum[i-1];
}
ll ans=inf;
for(int i=0;i<=n;i++)
{
ll cur=sum[i]*l+(sum[n]-sum[i])*r;
ll del=abs(n-i-i)-1;
if(del>0)
{
if(n-i>i) cur+=del*qr;
else cur+=del*ql;
}
if(cur<ans) ans=cur;
}
cout<<ans<<endl;
}
return 0;
}