B Marvolo Gaunt's Ring

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 31 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.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
typedef long long lli;
using namespace std;
const int  mxn = 1e9;
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
multiset<int> v;
multiset<int> :: iterator it ;
int main()
{
    TLE;
    lli n,cnt=1,m,k,ll;
    while(cin>>n>>m>>k>>ll)
    {
        int flag1 = 0,flag2=0;lli mx,x;
        if(m>=0 && k>=0 && ll>=0 )
            flag1=1;
        if(m<0 && k<0 && ll<0 )
            flag2=1;
        if(flag1)
        {
            cin>>mx;
            for(int i=2; i<=n; i++)
            {
                cin>>x;
                mx = max( mx ,x );
            }
            cout<<(m+k+ll)*mx<<endl;
        }
        else if(flag2)
        {
            cin>>mx;
            for(int i=2; i<=n; i++)
            {
                cin>>x;
                mx = min( mx ,x );
            }
            cout<<(m+k+ll)*mx<<endl;
        }
        else
        {

            lli dp[n+5];
            lli l[n+5],r[n+5];
            for(int i=1;i<=n;i++)
            {
                cin>>dp[i];
            }
            l[1] = m*dp[1];
            for(int i=2; i<=n; i++)
            {
                l[i] = max( l[i-1] ,m*dp[i] );
            }
            r[n] = ll*dp[n];
            for(int i=n-1; i>=1; i--)
            {
                r[i] = max( r[i+1] , ll*dp[i] );
            }
            mx = -1000000000000000000000-5;
            for(int i=1; i<=n; i++)
                mx = max( l[i]+r[i]+dp[i]*k , mx);
            cout<<mx<<endl;
        }
    }
    return 0;
}

B Marvolo Gaunt's Ring

原文地址:https://www.cnblogs.com/Shallow-dream/p/11623521.html

时间: 2024-10-09 16:05:05

B Marvolo Gaunt's Ring的相关文章

Codeforces 855B - Marvolo Gaunt&#39;s Ring

855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个值加起来的最大值. 代码: 代码①: #include<bits/stdc++.h> using namespace std; #define ll long long const int N=1e5+5; const int INF=0x3f3f3f3f; int a[N]; int premx

Codeforces 855B:Marvolo Gaunt&#39;s Ring(枚举,前后缀)

B. Marvolo Gaunt's Ring 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 sti

Marvolo Gaunt&#39;s Ring(巧妙利用前后缀进行模拟)

Description 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

CodeForces - 855B ring 前缀和

邓布利多教授正在帮助哈利摧毁魂器.当他怀疑一个魂器出现在那里时,他去了冈特沙克.他看到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(

HDU 1016 Prime Ring Problem(DFS)

题目链接 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

HDU 2296 Ring [AC自动机 DP 打印方案]

Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3536 Accepted Submission(s): 1153 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic

hdu 1016 Prime Ring Problem

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

自定义组合控件 圆环 ring

说明 可以设置内部填充或不填充 可以社会中边框颜色 View public class MyRingView extends RelativeLayout {     private Context mContext;     private ImageView iv_circle;     private ImageView iv_solid;     public MyRingView(Context context) {         super(context);         in