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

题意

给出一个有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(枚举,前后缀)的更多相关文章

  1. Codeforces 855B - Marvolo Gaunt's Ring

    855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...

  2. Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)

    Description Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as h ...

  3. B. Marvolo Gaunt's Ring 前缀后缀

    B. Marvolo Gaunt's Ring 这种一般只有三个的都可以处理前缀和后缀,再枚举中间这个值. 这个和之前写过的C. Four Segments 前缀后缀 处理方式很像. #include ...

  4. Codeforces 587D - Duff in Mafia(2-SAT+前后缀优化建图)

    Codeforces 题面传送门 & 洛谷题面传送门 2-SAT hot tea. 首先一眼二分答案,我们二分答案 \(mid\),那么问题转化为,是否存在一个所有边权都 \(\le mid\ ...

  5. 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring

    [链接]h在这里写链接 [题意] 给你n个数字; 让你在其中找出三个数字i,j,k(i<=j<=k); 使得p*a[i]+q*a[j]+r*a[k]最大; [题解] /*     有一个要 ...

  6. 【ST】【CF855B】 Marvolo Gaunt's Ring

    传送门 Description 给定三个数 \(p~,~q~,~r~\),以及一个数组 \(a\), 找出三个数 \(i~,~j~,~k\) ,其中 \(i~\leq~j~\leq~k\) 最大化 \ ...

  7. CodeForces - 855B ring 前缀和

    邓布利多教授正在帮助哈利摧毁魂器.当他怀疑一个魂器出现在那里时,他去了冈特沙克.他看到Marvolo Gaunt的戒指,并将其确定为魂器.虽然他摧毁了它,但仍然受到诅咒的影响.斯内普教授正在帮助邓布利 ...

  8. codeforces 579D D. "Or" Game(前后缀+贪心)

    题目链接: D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

随机推荐

  1. 《Scala编程》课程作业

    第一题.百元喝酒 作业要求:每瓶啤酒2元,3个空酒瓶或者5个瓶盖可换1瓶啤酒.100元最多可喝多少瓶啤酒?(不允许借啤酒) 思路:利用递归算法,一次性买完,然后递归算出瓶盖和空瓶能换的啤酒数 /** ...

  2. tomcat在eclipse上发布,Perference下的server找不到解决办法

    help--->Install New software得到如下所示 下面work with选项的内容与你的eclipse版本有关 我的eclipse版本为eclipse-java-2019-0 ...

  3. Ecshop 后台管理员密码忘记了吧~!~!~!

    方法1:把下面的代码保存为文件 mima.php <?php define('IN_ECS', true); require(dirname(__FILE__) . '/includes/ini ...

  4. ORACLE 按逗号拆分字符串为多行

    with t as (select '1,2,3,10,11,12' a from dual) select substr(a, decode(level - 1, 0, 0, instr(a, ', ...

  5. oc中调用c函数 实现将字符串转换成unsigned char

    帮助码友解决问题,从而复习了一下oc中调用c函数的方式 1,新建c 头文件  test.h 定义 c 函数 #ifndef test_h #define test_h void verificatio ...

  6. vue双向绑定和深浅拷贝

    现象描述: vue 在使用的时候,当table绑定了某个data的时候.假如某个el-table-column下面的有个方法传参(data.row),然后在方法中用一个obj=data.row.(这里 ...

  7. 带你尝鲜LiteOS 组件EasyFlash

    摘要:EasyFlash是一个开源的轻量级嵌入式闪存库. 本文分享自华为云社区<LiteOS组件尝鲜-玩转EasyFlash>,作者:Lionlace . 基本介绍 EasyFlash是一 ...

  8. 什么是Docker?

    容器技术的起源 假设你们公司正在秘密研发下一个"今日头条"APP,我们姑且称为明日头条,程序员自己从头到尾搭建了一套环境开始写代码,写完代码后程序员要把代码交给测试同学测试,这时测 ...

  9. Kafka从入门到放弃(三) —— 详说生产者

    上一篇对Kafka做了简单介绍,还没看的朋友可以点击下方链接. Kafka从入门到放弃(一) -- 初识别Kafka 消息中间件必须与生产者和消费者一起存在才有意义,这次先来聊聊Kafka的生产者. ...

  10. CF1093B Letters Rearranging 题解

    Content 有 \(t\) 次询问,每次询问给定一个字符串 \(s\).定义一个"好的字符串"为不是回文串的字符串.对于每一次询问,求出任意一个重新排列能够得到的"好 ...