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;
}

Codeforce 1155D Beautiful Array(DP)的更多相关文章

  1. 北邮校赛 I. Beautiful Array(DP)

    I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...

  2. zoj-3872 Beauty of Array (dp)

    ]Edward has an array A with N integers. He defines the beauty of an array as the summation of all di ...

  3. C. Ayoub and Lost Array(DP)

    (又是被队友带着上分的一场--) 题目链接:http://codeforces.com/contest/1105/problem/C 题目大意:给你n,l,r.每一个数都是在l,r范围之内,然后问你这 ...

  4. ACdream 1216 (ASC训练1) Beautiful People(DP)

    题目地址:http://acdream.info/problem? pid=1216 这题一開始用的是线段树.后来发现查询的时候还须要DP处理.挺麻烦..也就不了了之了..后来想到,这题事实上就是一个 ...

  5. 【CF55D】Beautiful numbers(动态规划)

    [CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...

  6. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  7. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  8. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  9. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

随机推荐

  1. go 基础安装

    一.安装: 1.下载GO的地址:https://golang.org/dl/ 点击安装包进行安装(linux直接解压) 设置环境变量(linux) 1. export GOROOT=$PATH:/pa ...

  2. Flask入门 之 endpoint

    首先,要纠正两个错误! 1.flask是通过endpoint找到viewfunction(视图函数的),并不是通过路由直接找到viewfunction的. 2.是url_for(endpoint)而不 ...

  3. Linux C++ 网络编程学习系列(3)——多路IO之poll实现

    poll实现多路IO 源码地址:https://github.com/whuwzp/linuxc/tree/master/poll 源码说明: server.cpp: 监听127.1:6666,功能是 ...

  4. 设置xml以让通知spring 扫描 注解

    <!--下边三个都是告诉spring扫描注解--> 一.<context:component-scan base-package="log.logback"/&g ...

  5. 提升效率必备!8个超好用的Python内置函数

    文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 吃着不想停 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...

  6. 装机摸鱼日志--ubuntu16.04安装网易云音乐客户端

    之前装的网易云音乐不指定啥原因不能用了,所以打算重新装一个,但是进官网只有deepin15和ubuntu18.04版本的安装包.然后我装了一下18.04的安装包,但是没有成功.甚至因为更换glibc差 ...

  7. 本地同时使用多个git账号

    config文件说明 Git Document指示在首次安装git的时候需要配置Config的相关内容信息,有三个地方存储了config文件,决定了读取的场景不同. 1 /etc/gitconfig: ...

  8. MYSQL和SQL Server 的区别

    注意MYSQL使用注释 -- 时 要后面加上空格 使用 #不用 一.数据类型 MYSQL:支持enum和set类型 ;SQL SERVER:不支持 MYSQL:不支持nchar,nvarchar,nt ...

  9. 理解RESTful API

    近日妹子向我求助RESTful API到底是个什么东西.原因是她们公司一个新启动的项目因为RESTful API起了争执.服务端同学坚持要用RESTful API,而前端同学则认为服务端用RESTfu ...

  10. ES6中对数值的扩展

    上一节和大家学习了字符串的扩展,这一节轮到了数值,我们一起来学习数值的扩展. 剧透一下,这一节并不会很烧脑,都是介绍类的知识讲解,理解性的东西不多,初学者们也可以愉快地看完. 传统的写法 在介绍数值的 ...