CodeForces - 855B (线段树维护区间最大值最小值)

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·ai + q·aj + r·ak最大,但要满足1 ≤ i ≤ j ≤ k ≤ n.这个的限制条件。所以我们可以用线段树维护区间最大最小值,扫一遍j,当p>=0的时候,查询(1,i)的最大值乘p,小于0时,查询(I,N)的最小值乘P,r同理。这样得到的值一定是最大的。最后取所有结果的最大值即可。

AC代码

#include
#include
#include
typedef long long ll;
using namespace std;
#define maxn 100000+100
#define INF 10000000000
ll ma[maxn<<2],mi[maxn<<2],a[maxn];
ll x;
void push(int rt)
{
    ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);
    mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%I64d",&x);
        ma[rt]=x;
        mi[rt]=x;
        a[l]=x;
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    push(rt);
}
ll querya(int l,int r,int rt,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return ma[rt];
    }
    ll m=-INF;
    int mid=(r+l)/2;
    if(L<=mid)m=max(querya(l,mid,rt<<1,L,R),m);
    if(R>mid)m=max(querya(mid+1,r,rt<<1|1,L,R),m);
    return m;
}
ll queryi(int l,int r,int rt,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return mi[rt];
    }
    ll m=INF;
    int mid=(r+l)/2;
    if(L<=mid)m=min(queryi(l,mid,rt<<1,L,R),m);
    if(R>mid)m=min(queryi(mid+1,r,rt<<1|1,L,R),m);
    return m;
}
typedef long long ll;
int main()
{
    ll n,p,q,r,ans=0,sum;
    scanf("%I64d%I64d%I64d%I64d",&n,&p,&q,&r);
    build(1,n,1);
    for(int i=1;i<=n;i++)
    {
        ans=a[i]*q;
        if(p>=0)ans+=querya(1,n,1,1,i)*p;
            else ans+=queryi(1,n,1,1,i)*p;
        //cout<=0)ans+=querya(1,n,1,i,n)*r;
            else ans+=queryi(1,n,1,i,n)*r;
       // cout<

看了别人的题解后发现可以用DP处理区间最大值,好像非常简洁Orz。。。

dp[0][i]是前i个p*a[i]的最大值,

dp[1][i]是在dp[0][i]的基础上加上q*a[i]的最大值,这样可以保证j>=i;

dp[2][i]是在dp[1][i]的基础上加上r*a[i]的最大值,这样可以保证k>=j;

太强了,比赛时完全想不到这种解法。

#include
#include
#include
typedef long long ll;
using namespace std;
#define maxn 100000+100
const ll INF = 8e18;
const int N = 1e5 + 10;
ll dp[3][N];
int a[N];
int main()
{
    ll p,q,r,n;
    cin>>n>>p>>q>>r;
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
    }
    dp[0][0]=-INF;
    dp[1][0]=-INF;
    dp[2][0]=-INF;
    for(int i=1;i<=n;i++)
    {
        dp[0][i]=max(dp[0][i-1],p*a[i]);
        dp[1][i]=max(dp[1][i-1],dp[0][i]+q*a[i]);
        dp[2][i]=max(dp[2][i-1],dp[1][i]+r*a[i]);
    }
    cout<

 

你可能感兴趣的:(CodeForces - 855B (线段树维护区间最大值最小值))