CodeForces

邓布利多教授正在帮助哈利摧毁魂器。当他怀疑一个魂器出现在那里时,他去了冈特沙克。他看到Marvolo Gaunt的戒指,并将其确定为魂器。虽然他摧毁了它,但仍然受到诅咒的影响。斯内普教授正在帮助邓布利多解除诅咒。为此,他想给Dumbledore提供他制作的药水x滴。

x的值被计算为给定p,q,r和阵列a1,a2,......的p·ai + q·aj + r·ak的最大值,使得1≤i≤j≤k≤n。帮助Snape找到x的值。请注意x的值可能是负数。

Input

第一行输入包含4个整数n,p,q,r( - 1e9≤p,q,r≤1e9, 1≤n≤1e5)。

下一行输入包含n个空格分隔的整数a1,a2,... an( - 1e9≤ai≤1e9)。

Output

输出p≤ai+ q·aj + r·ak的最大值,只要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.

题解,,前缀和,,用来保存偶从开始到当前状态的最大值,

              后缀和,用来保存从当前位置到结尾处的最大值。

应题目要求,i<=j<=k,所以前缀数组保存前i项p*a[i]最大值..后缀保存后从第n项到第i项r*a[i]的最大值

然后对中间项进行枚举,,求出最大值

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1E5+7;
const ll INF=-9223372036854775807+1;
ll arr[N];
ll before[N];
ll after[N];
int main(){
    int n;
    cin>>n;
    ll p,q,r;
    cin>>p>>q>>r;
    for(int i=1;i<=n;i++){
        scanf("%lld",&arr[i]);
    }
    for(int i=0;i<=n+1;i++){//数组的初始化,因为用到了最大值,所以要初始化为最小 
        before[i]=after[i]=INF;
    }
    
    
    for(int i=1;i<=n;i++){
        before[i]=max(before[i-1],p*arr[i]);//保存前i项的最大值 
    }
    
    
    for(int i=n;i>=1;i--){
        after[i]=max(after[i+1],r*arr[i]);//保存从第i项到第n项的最大值 
    }
     
     
    ll ans=INF;
    for(int i=1;i<=n;i++){
        ans=max(ans,before[i]+q*arr[i]+after[i]);
    }
    printf("%lld
",ans);
    
    return 0;
}

.

原文地址:https://www.cnblogs.com/Accepting/p/11373407.html