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. JDK7下VisualVm插件无法链接到插件中心

      VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量数据.跟踪内存泄漏.监控垃 ...

  2. Centos7 用yum命令安装LAMP环境(php+Apache+Mysql)以及php扩展

    1.yum -y update    // 更新系统 1.1)yum -y install gcc g++ gcc-c++ make kernel-devel kernel-headers 1.2)v ...

  3. Hibernate 注释用法

    注释 到现在为止,你已经看到 Hibernate 如何使用 XML 映射文件来完成从 POJO 到数据库表的数据转换的,反之亦然.Hibernate 注释是无需使用 XML 文件来定义映射的最新方法. ...

  4. python生成式和生成器

    一,生成式和生成器 1,列表生成式 mylist = [ i*i for i in range(3) if i>1 ] print(mylist) 运行结果:[4] 可以发现,快速生成列表的表达 ...

  5. Oracle EBS OPM convert dtl reservation

    --convert_dtl_reservation --created by jenrry DECLARE l_reservation_rec mtl_reservations%ROWTYPE; l_ ...

  6. SQL Server自动备份 备份到本地或者远程服务器

    0.1 在SQLServer2008 --> 备份数据库 --> 安全 --> 新建用户 --> 用户名 选择该windows用户 (确保 --> 机器名/人名 --&g ...

  7. Linux系统之TroubleShooting(启动故障排除)

    尽管Linux系统非常强大,稳定,但是我们在使用过程当中,如果人为操作不当,仍然会影响系统,甚至可能使得系统无法开机,无法运行服务等等各种问题.那么这篇博文就总结一下一些常见的故障排除方法,但是不可能 ...

  8. Windows Server 2012 R2 创建AD域

        前言 我们按照下图来创建第一个林中的第一个域.创建方法为先安装一台Windows服务器,然后将其升级为域控制器.然后创建第二台域控制器,一台成员服务器与一台加入域的Win8计算机. 环境 网络 ...

  9. Ctrl+Alt+F1~F6

    Ctrl+ALT+F1~F6 可以进入不同的字符终端和图形界面.体现了 linux 或者 unix 的多用户的特点. 6个不同的终端,相当于六个不同的用户. 保持更新,转载请著名出处.

  10. 【转】Java学习—什么是时间复杂度

    [原文]https://www.toutiao.com/i6593144782992704007/ 转载:程序员小灰 时间复杂度的意义 究竟什么是时间复杂度呢?让我们来想象一个场景: 某一天,小灰和大 ...