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. vi查找替换命令详解 (转载)

    转载至:   http://blog.csdn.net/lanxinju/article/details/5731843 一.查找 查找命令 /pattern<Enter> :向下查找pa ...

  2. 用UIScrollview做一个网易scrollviewbar

    效果如上,点击出现的图片是用UIImageview添加上的,比较简陋 我用了两种方法,第一种是直接在viewcontroller里面写代码 第二种是用了一个类来封装这个scrollviewbar 对外 ...

  3. 【Linux】【Services】【MessageQueue】搭建高可用rabbitMQ

    1. 简介 1.1. 官方网站: https://www.rabbitmq.com/ 1.2. 配置文档:https://docs.openstack.org/ha-guide/shared-mess ...

  4. my42_Mysql基于ROW格式的主从同步

    模拟主从update事务,从库跳过部分update事务后,再次开始同步的现象 主库 mysql> select * from dbamngdb.isNodeOK; +----+--------- ...

  5. Maven错误收集

    Eclipse 创建项目时报错 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1 ...

  6. Google Guava 常用集合方法

    /** * Author: momo * Date: 2018/6/7 * Description: */ public class ListTest { public static void mai ...

  7. CentOs 7 yum 安装Nginx

    打开官网下载文档:http://nginx.org/en/download.html 2进入操作系统 centOs 7,建立文件夹 nginx ,进入nginx ,拷贝 上图1编辑命令:/etc/yu ...

  8. 使用Lock接口来解决线程安全的问题

    package cn.itcast.demo16.Demo09.Lock;import java.util.concurrent.locks.Lock;import java.util.concurr ...

  9. 【经验分享】win10 cmake 构建 Tengine 工程

      欢迎关注我的公众号 [极智视界],回复001获取Google编程规范   O_o   >_<   o_O   O_o   ~_~   o_O   本教程详细记录了在 win10 环境中 ...

  10. Jenkins检测Maven项目是否引用快照包

    目录 一.简介 二.具体 一.简介 生产环境不允许使用快照包,但人为规定终究不如脚本进行检测,所以在打war包,检测是否引用了快照包,如果引用了宣布打包失败 二.具体 1.在pipeline的scri ...