Manthan, Codefest 17

来源:互联网 发布:淘宝联盟淘宝身份认证 编辑:程序博客网 时间:2024/05/16 10:46

Marvolo Gaunt’s Ring

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

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.

题意:给你三个数 p,q,r,然后给你给你一个有序的序列,让你在序列中跳出三个数i,j,k(i <=j<=k)使得 i*p+j*q+r*k 最大即可
分析:一开始没有搞懂题意,直接对数组排了序,取个最大最小,交了后发现题目没读懂,那天早起ing,直接睡觉去了,第二天发现还是没思路,结果一看了tourist神的,发现就是一个前缀后缀,还是没有仔细分析啊,

参考代码

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+7;ll a[maxn];ll pre_l[maxn],pre_r[maxn];int main(){    ll n,p,q,r;    cin>>n>>p>>q>>r;    for(int i = 0;i < n;i++)cin>>a[i];    pre_l[0] = a[0]*p;    pre_r[n-1] = a[n-1]*r;    for(int i = 1;i < n;i++)        pre_l[i] = max(pre_l[i-1],p*a[i]);    for(int i = n-2;i > -1;i--)        pre_r[i] = max(pre_r[i+1],r*a[i]);    ll ans =(ll)-9e18;    for(int i = 0;i < n;i++)        ans = max(ans,a[i]*q+pre_l[i]+pre_r[i]);    cout<<ans<<endl;    return 0;}
  • 如有错误或遗漏,请私聊下UP,ths