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. SDL 显示汉字

    #include "stdafx.h" #pragma comment(lib,"SDL.lib") #pragma comment(lib,"SDL ...

  2. 第3节 storm高级应用:1、上次课程回顾,今日课程大纲,storm下载地址、运行过程等

    上次课程内容回顾: ConcurrentHashMap是线程安全的,为什么多线程的时候还不好使,为什么还要加static关键字 1.storm的基本介绍:strom是twitter公司开源提供给apa ...

  3. 【拒绝挂分】盘点蒟蒻ghy的各种sb错误

    1.m与n打反(打错) NOIPd2t2 50变15爽不爽啊.jpg 某次信心赛四道sb题里面最sb的一道 sort里面的m打成n 100变40爽不爽啊.jpg(还有40我真的谢谢您了 2.没开lon ...

  4. WebRobot1.8.2网站多功能网络安全渗透检测工具

    新版介绍 最新版的Webrobot使用的是插件模式,插件存放在主程序目录下的plugin文件夹里,所有插件的配置文件及字典等其他文件也存放在这个文件夹内.我们需要用到哪个插件,只需要双击它便可打开,需 ...

  5. express写的接口在疯狂刷新几十次后,服务器挂掉

    用到的命令行: show status like 'Threads%'; show variables like '%max_connections%'; show global status lik ...

  6. mysql多实例双主部署

    本文引自公司技术文档,仅作为记录. 背景 有的时候需要进行数据库环境的隔离,以及节省服务器资源一台mysql安装多个数据库实例,一个实例下构建多个数据库​ 安装mysql yum -y install ...

  7. solus linux 中文输入法

    默认用ibus输入框架,安装ibus-libpinyin sudo eopkg install ibus-libpinyin ibus 安装好后重启 在系统设置 -区域和语言中添加中文,(记得自己设置 ...

  8. 解决phpinfo 和php -v版本不一致的问题

    解决方法:参考博文 http://blog.csdn.net/laomengnevergiveup/article/details/52104862

  9. 009-PHP循环输出数组成员

    <?php $Cities[] = "<B>北京</B>"; //等同于$Cities[0] = "北京" $Cities[] = ...

  10. IQueryable 转DataTable

    public static DataTable CopyToDataTable<T>(IEnumerable<T> array) { var ret = new DataTab ...