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. 汇编刷题:求1000H单元开始的10个无符号字节数的最大值(本题放入了BL寄存器)

    DATA SEGMENT ORG 1000H INFO DB 1,2,3,4,5,70H,71H,72H,80H,92H MAX DB 00H DATA ENDS CODE SEGMENT ASSUM ...

  2. lr使用soap协议,来对webservice接口进行测试

    实际项目中基于WSDL来测试WebService的情况并不多,WSDL并不是WebService测试的最佳选择. 最主要的原因还是因为WSDL文档过于复杂. 在案例(天气预报WebService服务) ...

  3. 教你爬取腾讯课堂、网易云课堂、mooc等所有课程信息

    本文的所有代码都在GitHub上托管,想要代码的同学请点击这里

  4. JAVA获取EXCEL列头

    FileInputStream fileInputStream = new FileInputStream(rootPath + path + "/" + fileName); L ...

  5. AJ学IOS(01) UI之Hello World与加法计算器

    不多说,AJ分享,必须精品 这两个一个是HelloWorld(左边) 另一个是 加法计算器(右边)的截图. 先运行第一个 程序看看效果 1.打开Xcode(没有哦mac系统的没有xcode的帮你们默哀 ...

  6. 如何练习python?有这五个游戏,实操经验就已经够了

    现在学习python的人越来越多了,但仅仅只是学习理论怎么够呢,如何练习python?已经是python初学者比较要学会的技巧了! 其实,最好的实操练习,就是玩游戏. 也许你不会信,但这五个小游戏足够 ...

  7. 三个步骤就能让你轻松掌握Python爬虫

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

  8. [PHP] 文件创建、写入、读取

    创建$p = fopen('text.txt','a+b'); 写入第一种方式//var_export方式存储数组到文件中 //这中方式存浮点型数据,存储后会多很多数字!只适合简单的存储吧!我感觉! ...

  9. [javascript]JS获取当前时间戳的方法

    JavaScript 获取当前时间戳: 第一种方法:(这种方法只精确到秒) var timestamp = Date.parse(new Date()); 结果:1280977330000 第二种方法 ...

  10. [linux] 小问题:管道符,换行问题等;[nginx]启动,重启,关闭命令;以及升级nginx切换命令

    Lniux换行问题 后面回车不会马上执行本条命令而是换行继续. : 是运行完前面就继续后面的, && 同样是前面正确就运行后面, || 是前面运行不正确就运行后面. | 管道符“|”将 ...