Description

You are given an array \(a\) consisting of \(n\) 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 \(a\) and multiply all values contained in this subarray by \(x\). You want to maximize the beauty of array after applying at most one such operation.

Input

The first line contains two integers \(n\) and \(x\) \((1≤n≤3⋅10^5,−100≤x≤100)\) — the length of array \(a\) and the integer \(x\) respectively.

The second line contains \(n\) integers \(a_1,a_2,…,a_n\) \((−10^9≤ai≤10^9)\) — the array \(a\).

Output

Print one integer — the maximum possible beauty of array \(a\) after multiplying all values belonging to some consecutive subarray \(x\).

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

Solution

  • \(d_1[i]\):以\(a[i]\)结尾的最大子段和
  • \(d_2[i]\):\(a[i]\)被乘以\(x\)且以\(a[i]\)结尾的最大子段和
  • \(d_3[i]\):\(a[i]\)没有被乘以\(x\),但在\(a[i]\)之前有一个区间被乘以\(x\),以\(a[i]\)结尾且包含该区间的最大子段和

答案就是\(\max\left(\max_{i=1}^{n}(d_1[i]), \max_{i=1}^{n}(d_2[i]),\max_{i=2}^{n}(d_3[i])\right)\)

转移方式在代码中:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<ll> a(n), d1(n), d2(n), d3(n);
for (int i = 0; i < n; ++i)
scanf("%I64d", &a[i]);
ll ans = 0;
for (int i = 0; i < n; ++i) {
d1[i] = a[i] + (i > 0 && d1[i - 1] > 0 ? d1[i - 1] : 0);
ans = max(ans, d1[i]);
}
for (int i = 0; i < n; ++i) {
d2[i] = k * a[i] + (i > 0 ? max(max(d2[i - 1], d1[i - 1]), 0LL) : 0);
ans = max(ans, d2[i]);
}
if (n > 1) ans = max(ans, d3[1] = a[0] * k + a[1]);
for (int i = 2; i < n; ++i) {
d3[i] = a[i] + max(d3[i - 1], d2[i - 1]);
ans = max(ans, d3[i]);
}
printf("%I64d\n", ans);
return 0;
}

CodeForces-1155D Beautiful Array的更多相关文章

  1. Codeforce 1155D Beautiful Array(DP)

    D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...

  2. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  3. Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  5. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  6. [Swift]LeetCode932. 漂亮数组 | Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  7. 漂亮数组 Beautiful Array

    2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...

  8. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  9. Codeforces 1077C Good Array 坑 C

    Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...

  10. LeetCode - Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

随机推荐

  1. Mysql使用优化之处(转)

    1 开启事务之前需要rollback 连接句柄.(清理垃圾)2 mysql_ping 失败,程序需要处理重连逻辑:3 mysql_query()执行的SQL语句是一个以‘/0’结尾的字符串,而mysq ...

  2. IIS日志导致磁盘被占满

    某服务器只部署了个IIS,应用目录都在D盘,可C盘97.5GB空间却被占满了. 将系统文件,隐藏文件全部显示,再选中所有的C盘文件及文件夹查看容量只有19GB. 既然只部署了IIS,那自然就怀疑到了I ...

  3. ELK 安装过程补充(不建议看,很少)

    1.yum 安装ELK服务 参考文档:https://blog.csdn.net/tonghudan/article/details/81414387 rpm -Uvh https://dl.fedo ...

  4. sql点滴45—mysql中group_concat用法

    group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果.比较抽象,难以理解. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组, ...

  5. 《面向对象程序设计》六 GUI

    git传送门 我这无药可救的拖延症和懒癌orz 主界面 文件读取界面 提示界面 最初选择vs+mfc,发现许多自动生成的代码读不懂(不须懂),尝试qt后感觉人生迎来了希望,看了推荐的视频与教程稍微了解 ...

  6. Windows Server 2012上安装.NET Framework 3.5

    引用:https://jingyan.baidu.com/article/14bd256e26b714bb6d26128a.html 装不成功后网上搜到很多相同的问题,都尝试过没解决到 用PowerS ...

  7. requirejs原理深究以及r.js和gulp的打包【转】

    转自:http://blog.csdn.net/why_fly/article/details/75088378 requirejs原理 requirejs的用法和原理分析:https://githu ...

  8. BZOJ1007:[HNOI2008]水平可见直线(计算几何)

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为 可见的,否则Li为被覆盖的. 例如,对于直线: L1:y ...

  9. Cobalt Strike深入使用

    System Profiler使用 System Profiler 模块,搜集目标的各类机器信息(操作系统版本,浏览器版本等) Attacks->web drive-by->System ...

  10. PSR规范0-4整理

    PSR规范 psr规范 引言: PSR 是 PHP Standard Recommendations 的简写,由 PHP FIG 组织制定的 PHP 规范,是 PHP 开发的实践标准.这些规范的目的是 ...