Codeforces 855B:Marvolo Gaunt'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 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.

题意

给出一个有n个数的序列a[1],a[2]……a[n],使得在i<=j<=k的条件下,令p*a[i]+q*a[j]+r*a[k]的值最大并输出这个值

思路

维护一个位置的前后缀left和right,left[i]表示位置i之前的元素与p相乘的最大值,right表示位置i之后的元素与q相乘的最大值,然后枚举每个位置,计算max(left[i]+q*a[i]+right([i])

代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=1e6+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 ll Left[maxn];
12 ll Right[maxn];
13 ll a[maxn];
14 int main(int argc, char const *argv[])
15 {
16     #ifndef ONLINE_JUDGE
17         freopen("/home/wzy/in.txt", "r", stdin);
18         freopen("/home/wzy/out.txt", "w", stdout);
19         srand((unsigned int)time(NULL));
20     #endif
21     ios::sync_with_stdio(false);
22     cin.tie(0);
23     int n;
24     ll p,q,r;
25     cin>>n>>p>>q>>r;
26     for(int i=0;i<n;i++)
27         cin>>a[i];
28     Left[0]=a[0]*p;
29     Right[n-1]=a[n-1]*r;
30     for(int i=1;i<n;i++)
31         Left[i]=max(Left[i-1],p*a[i]);
32     for(int i=n-2;i>=0;i--)
33         Right[i]=max(Right[i+1],r*a[i]);
34     ll ans=-INF;
35     for(int i=0;i<n;i++)
36         ans=max(ans,Left[i]+q*a[i]+Right[i]);
37     cout<<ans<<endl;
38     #ifndef ONLINE_JUDGE
39         cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
40     #endif
41     return 0;
42 }

Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)

原文地址:https://www.cnblogs.com/Friends-A/p/11372944.html

时间: 2024-10-01 07:45:09

Codeforces 855B: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

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

B Marvolo Gaunt&#39;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

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(

CodeForces 444C. DZY Loves Physics(枚举+水题)

转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/445/problem/C DZY Loves Physics DZY loves Physics, and he enjoys calculating density. Almost everything has density, even a graph. We define the densi

HDU 6186 CS Course【前后缀位运算枚举/线段树】

[前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm> #include<vector> #include<map

Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

#include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2;int nex[N];int cnt1[7],cnt2[7];int main(){    scanf("%s %s",s1+1,s2+1);    len1=strlen(s1+1);    len2=strlen(s2+1);    for(int i=1;i<=len1;i++

Hibernate给表和字段设置前后缀及分隔符

在<一口一口吃掉Hibernate(一)--使用SchemaExport生成数据表>中介绍了如何生成数据表.但是这只是最基本的.hibernate在生成或者操作数据库时,会受一些限制.比如一开始设计表的时候,直接写成了user(id,name,password,createTime)  这种格式的.但是客户后来要求表名称要以"t_"开头,字段名要以"stu_"开头,可以是一些数据库的关键字,单词之间以"_"隔开,方便他们维护.例如:T

#415 Div2 Problem C Do you want a data? (math &amp;&amp; 前后缀和 &amp;&amp; 快速幂)

题意: 首先定义集合的F值为  这个集合里面最大值和最小值的差. 现给出一个拥有n个数的集合(没有相同的元素), 要求求出这个集合内所有子集的F的值的和.例如: {4.7}这个集合里面有子集{4}.{7}.{4, 7}, 则这些子集的F值分别为4-4=0.7-7=0.7-4=3, 所以最后的结果就是0+0+3 = 3! 以下分析引用至 : http://blog.csdn.net/dragon60066/article/details/72599167 分析: 不难想到要先使数组升序方便计算和思