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

Sample Input

Input

5 1 2 3
1 2 3 4 5
Output

30
Input

5 1 2 -3
-1 -2 -3 -4 -5
Output

12
Hint

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.

      题意:找出最大的ai*p+aj*q+ak*r。保证i<=j<=k

       解析:目前我在各个博客中找到了三种解法(其实思想差不多),首先是前后缀数组模拟的:用L[]来记录 i 左边的最大a[]*p值,R[]来记录 i 右边的最大a[]*r最大值,q为中间,最后遍历的时候直接q*a[]来更新最大值就好了:

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;  //这注意了,1e18会WA,目前不知道原因
const int maxn = 1e5+;
ll a[maxn],L[maxn],R[maxn];
int main()
{
ll n,p,q,r;
cin>>n>>p>>q>>r;
for(int i=;i<=n;i++)
cin>>a[i];
L[]=a[]*p;
for(int i=;i<=n;i++)
L[i]=max(L[i-],a[i]*p);
R[n]=a[n]*r;
for(int i=n-;i>=;i--)
R[i]=max(R[i+],a[i]*r);
ll sum=-inf;
for(int i=;i<=n;i++)
sum=max(sum,L[i]+R[i]+q*a[i]);
cout<<sum<<endl;
}

    第二种:也是模拟,代码最为简单

#include <iostream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;
int main()
{
int n,p,q,r;
cin>>n>>p>>q>>r;
long long x;long long a=-inf,aa=-inf,aaa=-inf;
while(n--){
cin>>x;
a=max(a,p*x);
aa=max(aa,a+q*x);
aaa=max(aaa,aa+r*x);
}
cout<<aaa<<endl;
}

    第三种:按背包问题来做

#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
const int maxn=1e5+;
ll a[maxn],b[],dp[maxn][];
int main()
{
int n;
while(cin>>n)
{
for(int i=;i<=;i++)
cin>>b[i];
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
{
dp[i][]=;
for(int j=;j<=;j++)
dp[i][j]=-INF;
}
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
dp[i][j]=max(dp[i][j],max(dp[i-][j],dp[i][j-]+(ll)(a[i]*b[j])));
cout<<dp[n][]<<endl;
}
return ;
}

Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)的更多相关文章

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

    B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaun ...

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

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

  3. Codeforces 855B - Marvolo Gaunt's Ring

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

  4. 【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]最大; [题解] /*     有一个要 ...

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

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

  6. 一类巧妙利用利用失配树的序列DP

    I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...

  7. POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)

    题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...

  8. 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

    http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...

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

    题意: 首先定义集合的F值为  这个集合里面最大值和最小值的差. 现给出一个拥有n个数的集合(没有相同的元素), 要求求出这个集合内所有子集的F的值的和.例如: {4.7}这个集合里面有子集{4}.{ ...

随机推荐

  1. 以java实现的一个简单登录界面(带验证码)

    本文参考于:https://blog.csdn.net/wyf2017/article/details/78831744 https://blog.csdn.net/MengKun822/articl ...

  2. 第1节 storm编程:8、storm的分发策略

    8. Storm的分发策略 Storm当中的分组策略,一共有八种: 所谓的grouping策略就是在Spout与Bolt.Bolt与Bolt之间传递Tuple的方式.总共有八种方式: 1)shuffl ...

  3. 使用总结:java多线程总结 <转载>

    转载 https://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html java多线程总结 2011-08-28 20:08  Ro ...

  4. python+ selenium + webdriver的环境准备

    web自动化安装 1.安装最新的selenium pip install -U selenium 2.安装chrom浏览器和chromdriver的下载 http://chromedriver.sto ...

  5. 小程序真机上报错 for developer: some selectors are not allowed in component wxss , including tag name selectors, id selectors, and attribute selectors

    for developer: some selectors are not allowed in component wxss , including tag name selectors, id s ...

  6. 原型与继承与class

    对象有属性(专业点叫静态属性)和方法(专业点叫静态方法)和原型属性和原型方法 除了系统自带的那么几百万个对象,我们自己写在js的创建的对象,自定义的对象,都来自对象的构造函数,用来构造对象的函数,叫做 ...

  7. 7个现在就该学习Python 的理由【80%的人都不知道】

    Python 是一门更注重可读性和效率的语言,尤其是相较于 Java,PHP 以及 C++ 这样的语言,它的这两个优势让其在开发者中大受欢迎. 诚然,它有点老了,但仍是 80 后啊 —— 至少没有 C ...

  8. 困惑我的x++和++x;

    刚学习C语言时X++和++X非常不解 目前有了新的领悟 1.X++ int x=0; int z=x++; 此时z?x? 这个问题可以分两步思考 第一步:先把x的值赋予z,此时z=x=0; 第二步:x ...

  9. spring boot 接口service有多个实现类

    接口.java public interface DeService { } 接口实现类1.java @Service("ud")public class DeServiceImp ...

  10. netty权威指南学习笔记八——编解码技术之JBoss Marshalling

    JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...