- 题目链接: B.Marvolo Gaunt’s Ring
- 题意:
- 给出n, p, q, r
- 给出n个数, a[i]
- 求p * a[i] + q * a[j] + r * [k] 的最大值 ( 1<=i<=j<=k<=n)
- 解法:
- 限制条件i<=j<=k
- p*a[i] => 作为起点
- q*a[j] => 与 max( p * a[i] ) (i<=j) 相加
- r*a[k] => 与 max ( q * a[j] + p * a[i] ) (i<=j<=k) 相加
- 易知只需要三次循环,即可求出ans
using namespace std;
const int maxn = 1e5+7;
typedef long long ll;
int n;
ll a[maxn];
ll pn[maxn], qn[maxn], rn[maxn];
int main(){
ll n, p, q, r;
scanf("%lld %lld %lld %lld", &n, &p, &q, &r);
rep(i, 1, n){
scanf("%lld", &a[i]);
if(i==1){
pn[i] = p*a[i];
continue;
}
pn[i] = max(pn[i-1], p*a[i]);
}
rep(i, 1, n){
if(i==1) {
qn[i] = q*a[i] + pn[i];
continue;
}
qn[i] = max(qn[i-1], q*a[i]+pn[i]);
}
ll ans;
rep(i, 1, n){
if(i==1) {
rn[i] = r*a[i] + qn[i];
ans = rn[i];
continue;
}
rn[i] = max(rn[i-1], r*a[i]+qn[i]);
ans = max(ans, rn[i]);
}
printf("%lld\n", ans);
return 0;
}