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. Java第八天,抽象的概念是什么?如何完成抽象类的实现?

    抽象 面向对象编程中,抽象是一个很重要的概念,那么抽象有什么需要注意的地方呢?请熟记以下知识点. 如果父类当中的方法不确定如何进行方法体的实现,则这个方法就是抽象方法. 抽象方法只需要在方法前面加上a ...

  2. C语言 文件操作(四)

    1.fprintf int fprintf(FILE *stream, const char *format, ...) stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流 ...

  3. 31 Exception 异常处理

    /* * Exception in thread "main" java.lang.ArithmeticException: / by zero at com.itheima_01 ...

  4. "字母全变小写"组件:<lowercase> —— 快应用组件库H-UI

     <import name="lowercase" src="../Common/ui/h-ui/text/c_text_lowercase">& ...

  5. Jackson优化使用实例

    Jackson优化使用实例 博客分类: Java综合   JSON的三种处理方式  Jackson提供了三种可选的JSON处理方法(一种方式及其两个变型): 流式 API:(也称为"增量分析 ...

  6. jmeter 信息头Bearer

    1.数据规则 2.登录时获取token信息 3.正则表达式获取token值 说明: (1)引用名称:下一个请求要引用的参数名称,如填写title,则可用${title}引用它. (2)正则表达式: ( ...

  7. 怎么高效学习python?其实只需要这个方法,快速掌握不叫事儿

    很多人想学python,并且希望能快速高效的学习python,但一直都没有找到合适的方法,下面谈一下我的方法. 首先,高效入门python 怎么高效学习python?想要高效,就要先搞清楚你这个阶段, ...

  8. Vue中el-form标签中的自定义el-select下拉框标签

    页面写死el-select下拉框标签: 通过v-for="item in stateArr"绑定,stateArr声明在Vue组件里面的data参数里面代码如下: <el-f ...

  9. 【three.js第三课】鼠标事件,移动、旋转物体

    1.下载three.js的源码包后,文件夹结构如下: 2.在[three.js第一课]的代码基础上,引入OrbitControls.js文件,此文件主要用于 对鼠标的操作. 该文件位置:在文件结构中 ...

  10. python操作数据库-SQLSERVER-pyodbc

    刚开始学python时,大家都习惯用pymssql去读写SQLSERVER.但是实际使用过程中,pymssql的读写性能以及可靠性的确不如pyodbc来的好. 正如微软官方推荐使用pyodbc库,作为 ...