Codeforce - 855B - B. Marvolo Gaunt‘s Ring

本文讲述了哈利与邓布利多教授联手摧毁Horcrux的过程,他们如何识别并销毁了马尔福家族戒指,同时斯内普教授协助解除诅咒。关键在于求解最优配方,给邓布利多分配x滴药剂。解题涉及最大线性组合,注意x可能为负数。
摘要由CSDN通过智能技术生成

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.

刚开始看到这道题目的时候,我没有多想直接记下了数组中正负数的最大最小值,根据p、q、r的正负以及有无正负数来决定x要改变多少。

#include<bits/stdc++.h>
using namespace std;
long long a[100001];
int main()
{
	long long n,p[3];
	cin>>n>>p[0]>>p[1]>>p[2];
	long max1=-1,min1=-1,max2=1,min2=1;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if(a[i]>=0)
		{
			if(max1==-1||a[i]>max1) max1=a[i];
			if(min1==-1||a[i]<min1) min1=a[i];
		}
		if(a[i]<=0)
		{
			if(max2==1||a[i]>max2) max2=a[i];
			if(min2==1||a[i]<min2) min2=a[i];
		}
	}
	long long x=0;
	for(int i=0;i<3;i++)
	{
		if(p[i]>0)
		{
			if(max1!=-1) x+=(long long)p[i]*max1;
			else if(max2!=1) x+=(long long)p[i]*max2;
		}
		if(p[i]<0)
		{
			if(min2!=1) x+=(long long)p[i]*min2;
			else if(min1!=-1) x+=(long long)p[i]*min1;
		}
	}
	cout<<x<<endl;
}

测试完样例以后自信地提交,结果WA了。我当时就懵了,重新读了一遍题目,又还特意在有道上面翻译了一下,愣是没找出问题。

无奈之下,我只好去codeforce上搜题解,更懵了,倒也不是完全看不懂题解,但是题解比我的暴力解法复杂太多了。

后来我才发现,我忽视了1 ≤ i ≤ j ≤ k ≤ n,这个太关键了。意识到这一点之后,我豁然开朗。

之后我用了dp的做法(我也不确定这个算不算dp,毕竟我还没有学明白dp),但是第二个样例就不对了。

#include<bits/stdc++.h>
using namespace std;
long long a[100001]={};
long long dp[100001]={};
int main()
{
	long long n,p,q,k;
	cin>>n>>p>>q>>k;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) dp[i][0]=max(dp[i-1][0],p*a[i]);
	for(int i=1;i<=n;i++) dp[i][1]=max(dp[i-1][1],dp[i][0]+q*a[i]);
	for(int i=1;i<=n;i++) dp[i][2]=max(dp[i-1][2],dp[i][1]+k*a[i]);
	cout<<dp[n][2]<<endl;
    return 0;
}

我又琢磨了一会,然后发现,题目中特意强调了x可以是负数,而我打着打着,忘记了。

#include<bits/stdc++.h>
using namespace std;
const long long _INF=-8e18;
long long dp[100005][3]={};
long long a[100005]={};
int main()
{
	long long n,p,q,k;
	cin>>n>>p>>q>>k;
	for(int i=1;i<=n;i++) cin>>a[i];
	dp[0][0]=_INF;
    dp[0][1]=_INF;
    dp[0][2]=_INF;
	for(int i=1;i<=n;i++) dp[i][0]=max(dp[i-1][0],p*a[i]);
	for(int i=1;i<=n;i++) dp[i][1]=max(dp[i-1][1],dp[i][0]+q*a[i]);
	for(int i=1;i<=n;i++) dp[i][2]=max(dp[i-1][2],dp[i][1]+k*a[i]);
	cout<<dp[n][2]<<endl;
	return 0;
}

以上是dp的做法。至于其它做法,像是线段树啊之类的,等我学了相应的知识点,要是还记得,就回来继续补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值