前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforce 1155D Beautiful Array(DP)

Codeforce 1155D Beautiful Array(DP)

作者头像
风骨散人Chiam
发布2020-10-28 10:14:55
2740
发布2020-10-28 10:14:55
举报
文章被收录于专栏:CSDN旧文

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

代码语言:javascript
复制
5 -2
-3 8 -2 1 -6

output

代码语言:javascript
复制
22

input

代码语言:javascript
复制
12 -3
1 3 3 7 1 3 3 7 1 3 3 7

output

代码语言:javascript
复制
42

input

代码语言:javascript
复制
5 10
-1 -2 -3 -4 -5

output

代码语言:javascript
复制
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,那么显然划分成三段,于是有了三部分,每一部分都按照自己的状态转移方程写到一起,比较最大值,就可以得到那一段我该如何处理。

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档