Codeforces 1155D

Each value of a given sequence may be a contiguous subsequences being multiplied by x, the maximum value of the operation to ask the contiguous subsequence after how much can be obtained.
DP can engage,
DP [i] [0] indicates the i when there is no using the value obtained by multiplying the maximum continuous subsequence x operation can be obtained DP [I] [0] = max (a [I], DP [I-. 1] [0] + a [I])
DP [I] [. 1] represents the maximum value is in the contiguous subsequence by operation of x to i can be obtained when
the dp [i] [1] = max (dp [i] [1], max (dp [i-1] [ 0], DP [I-. 1] [. 1]) + a [I] * x)
DP [I] [2] represented has passed by x operating interval
dp [i] [2] = max (dp [i ] [2], max (dp [i-1] [1], dp [i-1] [2]) + a [i])
to pay attention to the problem of burst int

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5 +10;
int x,n,a[N];
LL dp[N][3];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n>>x;
	for(int i = 1;i<=n;i++) cin>>a[i];
	for(int i = 1;i<=n;i++){
		dp[i][0] = max((LL)a[i],dp[i-1][0] + a[i]);
		dp[i][1] = max(dp[i][1],max(dp[i-1][0],dp[i-1][1]) + (LL)a[i] * x);
		dp[i][2] = max(dp[i][2],max(dp[i-1][1],dp[i-1][2])+ a[i]);
	}
	LL ans = 0;
	for(int i = 1;i<=n;i++)
		ans = max(max(ans,dp[i][0]),max(dp[i][1],dp[i][2]));
	cout<<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/winhcc/article/details/89968870