Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)

Posted friends-a

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)相关的知识,希望对你有一定的参考价值。

B. Marvolo Gaunt‘s Ring

Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt‘s Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made.

Value of x is calculated as maximum of p·ai + q·aj + r·ak for given p, q, r and array a1, a2, ... an such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.

Input

First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105).

Next line of input contains n space separated integers a1, a2, ... an ( - 109 ≤ ai ≤ 109).

Output

Output a single integer the maximum value of p·ai + q·aj + r·ak that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.

Examples

input
5 1 2 3
1 2 3 4 5
output
30
input
5 1 2 -3
-1 -2 -3 -4 -5
output
12

Note

In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.

In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.

题意

给出一个有n个数的序列a[1],a[2]……a[n],使得在i<=j<=k的条件下,令p*a[i]+q*a[j]+r*a[k]的值最大并输出这个值

思路

维护一个位置的前后缀left和right,left[i]表示位置i之前的元素与p相乘的最大值,right表示位置i之后的元素与q相乘的最大值,然后枚举每个位置,计算max(left[i]+q*a[i]+right([i])

代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=1e6+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 ll Left[maxn];
12 ll Right[maxn];
13 ll a[maxn];
14 int main(int argc, char const *argv[])
15 
16     #ifndef ONLINE_JUDGE
17         freopen("/home/wzy/in.txt", "r", stdin);
18         freopen("/home/wzy/out.txt", "w", stdout);
19         srand((unsigned int)time(NULL));
20     #endif
21     ios::sync_with_stdio(false);
22     cin.tie(0);
23     int n;
24     ll p,q,r;
25     cin>>n>>p>>q>>r;
26     for(int i=0;i<n;i++)
27         cin>>a[i];
28     Left[0]=a[0]*p;
29     Right[n-1]=a[n-1]*r;
30     for(int i=1;i<n;i++)
31         Left[i]=max(Left[i-1],p*a[i]);
32     for(int i=n-2;i>=0;i--)
33         Right[i]=max(Right[i+1],r*a[i]);
34     ll ans=-INF;
35     for(int i=0;i<n;i++)
36         ans=max(ans,Left[i]+q*a[i]+Right[i]);
37     cout<<ans<<endl;
38     #ifndef ONLINE_JUDGE
39         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
40     #endif
41     return 0;
42 

 

以上是关于Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 855B Marvolo Gaunt's Ring

Codeforces 855B - Marvolo Gaunt's Ring

Codeforces 855B 简单DP

CodeForces - 855B ring 前缀和

B Marvolo Gaunt's Ring

CodeForce 855B 暴力or线段树