dp--最大区间和变形-cf-1155D

D. Beautiful Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xxrespectively.

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

Copy

5 -2
-3 8 -2 1 -6

output

Copy

22

input

Copy

12 -3
1 3 3 7 1 3 3 7 1 3 3 7

output

Copy

42

input

Copy

5 10
-1 -2 -3 -4 -5

output

Copy

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.

状态转移方程如下

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>
#include <climits>
#include <fstream> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 300005;
const int MOD = 1e9 + 7; #define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i) int main()
{
LL n, m, t, ans = 0, p = 0, sum1 = 0, sum2 = 0;
cin >> n >> m;
F(i, 1, n)
{
cin >> t;
sum1 = max(sum1 + t, 0ll);//d[i][0] = max(0ll,d[i-1][0]+a[i]);
sum2 = max(sum2 + m * t, sum1);//d[i][1] = max( 0ll, max(d[i-1][0],d[i-1][1])+a[i]*x);
p = max(p + t, sum2);// d[i][2] = max( 0ll, max(d[i-1][0], max(d[i-1][1],d[i-1][2]) )+ a[i]);
ans = max(ans, p);
}
cout << ans << endl;
return 0;
}

dp--最大区间和变形-cf-1155D的更多相关文章

  1. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. hihocoder1636 Pangu and Stones(区间DP(石子合并变形))

    题目链接:http://hihocoder.com/problemset/problem/1636 题目大意:有n堆石头,每次只能合并l~r堆,每次合并的花费是要合并的石子的重量,问你合并n堆石子的最 ...

  3. Vijos1451圆环取数[环形DP|区间DP]

    背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...

  4. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  5. dp之区间:Light oj 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多 ...

  6. Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp

    B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. 区间DP(区间最优解)题目讲解总结

    1:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度. [分析]区间DP要覆盖整个区间,那么要求所有情况的并集. 先想出状态方程: dp[i][j]:i ~ j区间内最大匹配数目 输出:dp ...

  8. 「DP」区间dp

    区间dp? 状态设计为描述一段区间的dp. eg:f[i][j]表示从 i 到 j 这个区间上的最优值.

  9. lightoj 1422【区间DP·分类区间首元素的情况】

    题意: 给你n天分别要穿的衣服种类,可以套着穿, 一旦脱下来就不能再穿,求n天至少要几件. 思路: 区间DP dp[i][j]代表i到j需要至少几件衣服 第i天的衣服在第i天穿上了,dp[i][j]= ...

随机推荐

  1. sklearn中决策树算法DesiciontTreeClassifier()调用以及sklearn自带的数据包sklearn.datasets.load_iris()的应用

    决策树方法的简单调用记录一下 clf=tree.DecisionTreeClassifier() dataMat=[];labelMat=[] dataPath='D:/machinelearning ...

  2. C语言基础课First作业

    一.大学和高中最大的不同是没有人天天看着你,也不会担心上课的时候班主任在后门偷偷瞄着我们,通过阅读邹欣老师的博客后,心目中理想的师生关系就是Coach/Trainee(健身教练/健身学员)的关系,想到 ...

  3. windows7 不能更新,提示:"WindowsUpdate_80240016" "WindowsUpdate_dt000",如何解决?

    计算机(右键) ---- 管理 -------- 服务和应用程序 -----服务(找到名称为windows update的服务,并且在windwos update服务右键 选择重新启动 ) 再次安装更 ...

  4. Array对象的创建及其操作方法

    一.创建数组,即实例化数组对象      有三种方式:1. new Array();                          2.new Array(size);               ...

  5. ETL开发

    要进入开发阶段,了解不同的ETL产品. 整个ETL系统中,时间或更精确的,吞吐量是主要关心的内容.这种转换处理任务设计的主要目的归根结底是使得数据装载到展现表中最快并使得最终用户能快速的从这些表中得到 ...

  6. Android-HttpsURLConnectionHelp工具类

    Java版(HttpURLConnection)Https请求工具类 public class HttpsURLConnectionHelp { /** * 加密协议 */ public static ...

  7. struts2设置index.action为主页(另:web.xml编辑卡死问题解决)

    本来是弄拦截器的问题,结果弄主页的时候,还是发现了问题. 公司网站的项目里面,是用index.action作为主页的,访问WEB-INF里面的html文件.可是我设置的却不成功,追根到底,一个原因,s ...

  8. Linux服务器使用XShell上传下载文件

    在学习Linux过程中,我们常常需要将本地文件上传到Linux主机上,这里简单记录下使用Xsheel工具进行文件传输 1:首先连接上一台Linux主机 2:输入rz命令,看是否已经安装了lrzsz,如 ...

  9. [Erlang36]kerl轻松管理安装各种OTP版本

    kerl只有一个目标:让我们在不同的OTP版本间随意切换.他是一个纯Bash项目.简单实用的工作利器! Readme里面用法已说明得非常清楚了.建议按流程来一次. 1.下载 安装(一个bash脚本,根 ...

  10. XML--将XML中数据提取出转换成表2

    DECLARE @xml XMLSET @xml = '<Students>    <Student  id="1001" name = "xu&quo ...