D. Beautiful Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.
You may choose at most one consecutive subarray of aa and multiply all values contained in this subarray by xx. You want to maximize the beauty of array after applying at most one such operation.
Input
The first line contains two integers nn and xx (1≤n≤3⋅105,−100≤x≤100) — the length of array aa and the integer xx respectively.
The second line contains nn integers a1,a2,…,an (−109≤ai≤109) — the array aa.
Output
Print one integer — the maximum possible beauty of array aa after multiplying all values belonging to some consecutive subarray x.
Examples
input
5 -2 -3 8 -2 1 -6
output
22
input
12 -3 1 3 3 7 1 3 3 7 1 3 3 7
output
42
input
5 10 -1 -2 -3 -4 -5
output
0
Note
In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22([-3, 8, 4, -2, 12]).
In the second test case we don‘t need to multiply any subarray at all.
In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
果然还是太菜了,简单dp都想不出来。
dp[0] 记录不使用x的最大值
dp[1] 记录允许使用x的情况下的最大值
dp[2]记录在前面的区间中已经使用了x的情况下的最大值(尽管可能使用x的区间对最大值并没有贡献)
#include<cstdio> #include<algorithm> using namespace std; long long arr[300005]; int main(){ int n, x; long long dp[3] = {},ans=0; scanf("%d%d",&n,&x); for(int i=0;i<n;i++){ scanf("%I64d",&arr[i]); dp[0] = max(0ll,dp[0]+arr[i]); dp[1] = max(dp[0],dp[1]+arr[i]*x); dp[2] = max(dp[1],dp[2]+arr[i]); ans = max(ans,dp[2]); } printf("%I64d\n",ans); return 0; }
原文地址:https://www.cnblogs.com/kongbb/p/10758535.html