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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to maximize pa_i + qa_j + ra_k subject to i ≤ j ≤ k. We can treat three positions. Since constraints up to n=1e5, we need O(n) or O(n log n). There is classic problem "maximize pai + qaj + rak with i <= j <= k" can be solved by precomputing prefix maximum for pai and suffix maximum for rak, and then iterate j to compute pai_max + qaj + rak_max. But careful: p, q, r can be negative, and a_i can be negative. The condition i <= j <= k means we need to choose i before j and k after j. So for each j, we need the maximum of pa_i for i ≤ j, and maximum of ra_k for k ≥ j. However, because p and r can be negative, the "maximum" might not be simply pa_i maximum if we want to maximize sum? Actually, we want maximize total = pa_i + qa_j + ra_k. For fixed j, the contribution from i part is pa_i, from k part is ra_k. Since i and k are independent (subject to i ≤ j ≤ k), to maximize total for a given j, we should choose i (≤ j) that maximizes pa_i, and k (≥ j) that maximizes ra_k. Indeed, because the sum is separable: for fixed j, we can separately choose best i and best k. So if we precompute left_max[j] = max_{i ≤ j} pa_i, and right_max[j] = max_{k ≥ j} ra_k, then answer = max_{j} (left_max[j] + qa_j + right_max[j]). However, note that i and k must be such that i ≤ j ≤ k; left_max[j] ensures i ≤ j, right_max[j] ensures k ≥ j. So it's correct.
But caution: p, q, r can be negative. left_max[j] = max(pa_i) over i ≤ j. That's fine. Similarly right_max. But if p is negative, we are essentially maximizing pa_i which is a large negative times a_i? Actually, if p is negative, maximizing p*a_i may yield a relatively high value if a_i is negative (since negative times negative positive), so it's fine. This is just max of expression. Similarly for r.
We must consider that the maximum might be negative if all combos are negative, but we just compute.
Thus solution: compute array a[1..n]. Compute left[i] = max( p * a_k for k=1..i ). We can do: left[1]=pa1; for i=2..n: left[i]=max(left[i-1], pa_i). Similarly, compute right[i] = max( r * a_k for k=i..n ). right[n]=ra_n; for i=n-1..1: right[i]=max(right[i+1], ra_i). Then for each j from 1 to n, compute total = left[j] + q*a_j + right[j]. Keep max. Output max. Complexity O(n). Works.
Edge cases: n up to 1e5, values up to 1e9, p,q,r up to 1e9, product can be up to 1e18, so need 64-bit integer. Use long long.
But wait: The condition is i ≤ j ≤ k. In our approach, we may pick i and k possibly both equal to j, but that's allowed. However, could there be a scenario where the best i for a given j is after the best k? No, they are independent.
But is there any subtlety: When we use left_max[j] = max_{i≤j} p*a_i, we might be using an i that is not necessarily <= j? That's fine. Similarly for right_max. So it's correct.
But check second example: n=5, p=1,q=2,r=-3; array: -1,-2,-3,-4,-5. left_max: compute pa_i = a_i (since p=1). So left_max: [-1, -1, -1, -1, -1] because a1=-1, a2=-2 => max(-1,-2) = -1; similarly all -1. right_max: ra_k = -3 * a_k. Compute for each: k=1: -3*-1=3, k=2: -3*-2=6, k=3: -3*-3=9, k=4: -3*-4=12, k=5: -3*-5=15. So right_max from j: suffix max: for j=1: max from 1..5 = 15? Actually suffix max: right[5]=15, right[4]=max(ra4=12, right[5]=15)=15, right[3]=max(9,15)=15, right[2]=max(6,15)=15, right[1]=max(3,15)=15. So right_max all 15. Then for each j: total = left_max[j] + qa_j + right_max[j] = -1 + 2a_j + 15 = 14 + 2a_j. For j=1: 14+2*(-1)=12, j=2: 14+2*(-2)=10, j=3:14-6=8, j=4:14-8=6, j=5:14-10=4. Max is 12, which matches output. Good.
But what about scenario where p and r are both negative, and we might want to minimize pa_i? No, we want maximum sum. So we want the largest pa_i, even if negative. If all p*a_i are negative, then the max is the least negative, which is correct. The left_max picks that.
Thus solution is straightforward.
Potential pitfalls: if n is 1? Then i=j=k=1. left[1]=pa1, right[1]=ra1, total = pa1+qa1+r*a1. Works.
Note: Values can be up to 1e9, p,q,r up to 1e9, product up to 1e18, sum up to maybe 3e18, within signed 64-bit range (max ~9.22e18). So use long long.
Implementation steps:
read n, p, q, r (int).
read array a (list of ints).
compute left array of length n: left[0]=p * a[0]; for i=1..n-1: left[i]=max(left[i-1], p*a[i]).
compute right array: right[n-1]=ra[n-1]; for i=n-2..0: right[i]=max(right[i+1], ra[i]).
initialize ans = left[0] + q*a[0] + right[0] (or a very small number, like -9e18).
iterate j from 0 to n-1: total = left[j] + q*a[j] + right[j]; ans = max(ans, total).
output ans.
Check if any more constraints: i ≤ j ≤ k strictly, but allowed equal. So fine.
Alternative approach: dynamic programming style but O(n). This is typical.
Now we also consider possibility that we need to consider i, j, k distinct? No, allowed equal.
Thus answer is done.
But wait: Could there be a scenario where the best i for given j is not necessarily the max of pa_i because of some dependency with k? No, because for fixed j, i and k are independent; the sum is pa_i + (qa_j) + ra_k. The term pa_i does not interact with k, and ra_k not with i. So independently choosing max for each gives max total for that j. So it's optimal.
Thus solution is correct.
But I recall a similar problem "Maximum sum of three terms with order constraints" often solved by DP with three states, but prefix-suffix works.
Potential overflow: p*a_i: p up to 1e9, a_i up to 1e9 => up to 1e18. In Python we have arbitrary integers, but in C++ need long long. Use long long.
Now ensure we handle negative values: max function works.
Edge: If n=0? Not possible, n>=1.
Thus answer.
We'll produce final answer.
But the problem statement includes -10^9 ≤ p,q,r ≤ 10^9. So okay.
Now we must output as integer.
Now I will produce final code in C++.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n; ll p,q,r; // but p,q,r up to 1e9, can fit in int, but use ll for multiplication.
cin>>n>>p>>q>>r;
vector a(n);
for(int i=0;i<n;i++) cin>>a[i];
vector left(n), right(n);
left[0]=pa[0];
for(int i=1;i<n;i++) left[i]=max(left[i-1], pa[i]);
right[n-1]=ra[n-1];
for(int i=n-2;i>=0;i--) right[i]=max(right[i+1], ra[i]);
ll ans = left[0] + qa[0] + right[0]; // or LLONG_MIN
for(int j=0;j<n;j++) {
ll total = left[j] + qa[j] + right[j];
if(total>ans) ans=total;
}
cout<<ans<<endl;
return 0;
}
But careful: When p, q, r are negative and a_i large negative, product might be positive but could overflow 64-bit? 1e9*1e9=1e18, which fits in signed 64-bit (max ~9.22e18). So safe.
But consider p = -1e9, a_i = -1e9, product = 1e18, still ok. So long long is fine.
Now need to import bits/stdc++.h or iostream.
Make sure to use long long for all.
Alternative: Use LLONG_MIN for initial ans, but if all sums are negative and we initialize with first, that's fine; but we can also set ans = -3e18? Safer: ans = LLONG_MIN from . But okay.
Thus final answer.
I'll present as solution.
But note: The problem might be from Codeforces? Might be "Marvolo Gaunt's Ring"? I recall a problem "Marvolo Gaunt's Ring" from Codeforces: Maximize pai + qaj + r*ak with i ≤ j ≤ k. Indeed, typical solution is prefix-suffix.
But the trick: There is also a variant where p, q, r can be negative and we need to be careful because it's not simply max of pai on left and max of rak on right. Actually, the above reasoning holds. However, there is nuance: The expression is pa_i + qa_j + r*a_k. For fixed j, left part is independent and right part independent. So it's correct.
Double-check with random test: Suppose p = -1, q = -1, r = -1; a = [1,2,3]. left_max: pa = [-1,-2,-3] => left: [-1,-1,-1]? Actually max of [-1] is -1, for i=2: max(-1, -2) = -1, for i=3: max(-1, -3) = -1. right_max: ra = [-1,-2,-3] suffix max: [-1,-1,-1]? For j=3: -3; j=2: max(-2,-3) = -2? Wait suffix max for j=2: over indices 2..3: -2 and -3, max = -2. But that's not consistent? Let's compute properly: a1=1, a2=2, a3=3. p=-1 => pa: -1, -2, -3. left[1]=-1, left[2]=max(-1, -2)=-1, left[3]=max(-1, -3)=-1. So left = [-1,-1,-1] for indices 1..3. right: start from end: right[3] = -3, right[2] = max(-2, right[3]=-3) = -2, right[1] = max(-1, right[2]=-2) = -1. So right = [-1,-2,-3] for indices 1..3. Then total for j=1: left[1] + qa1 + right[1] = -1 + (-1*1) + (-1) = -3; j=2: -1 + (-2) + (-2) = -5; j=3: -1 + (-3) + (-3) = -7. Max = -3. Now check all combos: i<=j<=k. list combos: (1,1,1): -1-1-1=-3; (1,1,2): -1-1-2=-4; (1,1,3): -1-1-3=-5; (1,2,2): -1-2-2=-5; (1,2,3): -1-2-3=-6; (1,3,3): -1-3-3=-7; (2,2,2): -2-2-2=-6; (2,2,3): -2-2-3=-7; (2,3,3): -2-3-3=-8; (3,3,3): -3-3-3=-9. Maximum is -3. So matches. Good.
Thus approach works.
Now there is alternative solution: compute left best including q? But no.
Thus answer.
We'll produce final solution.
Potential pitfalls: Using int for p,q,r but then multiplication might overflow before casting to long long. So read as long long.
Better: read n as int, p,q,r as long long. Or read all as long long.
We'll implement.
Edge: n=1, left[0]=pa[0], right[0]=ra[0], total = pa[0] + qa[0] + r*a[0]. OK.
Now code.
I'll also use fast I/O maybe.
Complexity O(n).
Now final.
But before final answer, need to consider if there is any scenario where left_max[j] and right_max[j] could be from the same index? Actually, left_max[j] picks some i <= j, right_max[j] picks some k >= j. They could be the same index if j is that index, but that's fine. They could also be different; no conflict.
Thus solution is correct.
Now produce final answer with explanation.
But as ChatGPT, I need to output the final code snippet.
Thus answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
code
int n;
ll p, q, r;
cin >> n >> p >> q >> r;
vector<ll> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<ll> left(n), right(n);
// prefix maximum of p * a[i]
left[0] = p * a[0];
for (int i = 1; i < n; ++i) {
left[i] = max(left[i - 1], p * a[i]);
}
// suffix maximum of r * a[i]
right[n - 1] = r * a[n - 1];
for (int i = n - 2; i >= 0; --i) {
right[i] = max(right[i + 1], r * a[i]);
}
// compute answer by iterating over j
ll ans = left[0] + q * a[0] + right[0]; // initialise with j = 0
for (int j = 0; j < n; ++j) {
ll cur = left[j] + q * a[j] + right[j];
if (cur > ans) ans = cur;
}
cout << ans << "\n";
return 0;