Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array
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≤1001≤n≤3⋅105,−100≤x≤100) — the length of array aa and the integer xx respectively.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−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 xx.
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.
这个题要分成三个阶段考虑最大值,第一个阶段单纯累加最大,第二部分累加K*a【i】最大,第三部分是第二部分结束后进行第一种累加,显然需要找一段乘K,那么显然划分成三段,于是有了三部分,每一部分都按照自己的状态转移方程写到一起,比较最大值,就可以得到那一段我该如何处理。
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define INF 0x3f3f3f3f
#define maxn 100010
#define esp 1e-9
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long ll;
ll N,M,max1,max2,max3,ans,x;
int main()
{
cin>>N>>M;
max1=max2=max3=ans;
for(ll i=0; i<N; i++)
{
cin>>x;
max1=max(0LL,(max1+x));//当M为负数时
max2=max(max1,(max2+M*x));
max3=max(max2,(max3+x));
//最大只可能出在max1,max2,max3
//max1 最大正数字段和
//max2 最大乘以M字段和
//max3 某子段陈M结束后最大字段和
ans=max(ans,max3);
}
cout<<ans<<endl;
}