Problem Statement

You are given an integer sequence of length $N$: $A=(A_1,A_2,\ldots,A_N)$.

You will perform the following consecutive operations just once:

  • Choose an integer $x$ $(0\leq x \leq N)$. If $x$ is $0$, do nothing. If $x$ is $1$ or greater, replace each of $A_1,A_2,\ldots,A_x$ with $L$.

  • Choose an integer $y$ $(0\leq y \leq N)$. If $y$ is $0$, do nothing. If $y$ is $1$ or greater, replace each of $A_{N},A_{N-1},\ldots,A_{N-y+1}$ with $R$.

Print the minimum possible sum of the elements of $A$ after the operations.

Constraints

  • $1 \leq N \leq 2\times 10^5$
  • $-10^9 \leq L, R\leq 10^9$
  • $-10^9 \leq A_i\leq 10^9$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $L$ $R$
$A_1$ $A_2$ $\ldots$ $A_N$

Output

Print the answer.


Sample Input 1

5 4 3
5 5 0 6 3

Sample Output 1

14

If you choose $x=2$ and $y=2$, you will get $A = (4,4,0,3,3)$, for the sum of $14$, which is the minimum sum achievable.


Sample Input 2

4 10 10
1 2 3 4

Sample Output 2

10

If you choose $x=0$ and $y=0$, you will get $A = (1,2,3,4)$, for the sum of $10$, which is the minimum sum achievable.


Sample Input 3

10 -5 -3
9 -6 10 -1 2 10 -1 7 -15 5

Sample Output 3

-58

$L$, $R$, and $A_i$ may be negative.

设把区间 \([1,l-1]\) 全部变成 \(L\),把区间 \([r+1,n]\) 全部变成 \(R\),其余不变。预处理出数列 \(a\) 的前缀和 \(s\).

此时的总价值为 $$(s_r-s_{l-1})+L(l-1)+R(n-r)$$

\[=(s_r-rR-(s_{l-1}-lL))+Rn-L
\]

枚举 \(r\),要找到最大的 \(s_{l-1}-lL\),在枚举的同时维护最小值就可以了。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,l,r,a[N];
long long s[N],t[N],ans=1e18;
int main()
{
scanf("%d%d%d",&n,&l,&r);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
s[i]=s[i-1]+a[i];
ans=min(ans,1LL*i*l+1LL*(n-i)*r);
}
ans=min(ans,s[n]);
for(int i=0;i<=n;i++)
t[i]=max(t[i-1],s[i]-1LL*i*l);
for(int i=0;i<=n;i++)
ans=min(ans,s[i]-t[i-1]+1LL*n*r-1LL*i*r);
printf("%lld",ans);
}

[ABC263D] Left Right Operation的更多相关文章

  1. 终端mysql Operation not permitted错误解决方案

    前言 前段时间装mysql,就遇到了ln: /usr/bin/mysql: Operation not permitted的错误,网上好多方法都过时了,下边是我的解决方法 原因 这是因为苹果在OS X ...

  2. SVN:Previous operation has not finished; run 'cleanup' if it was interrupted

    异常处理汇总-开发工具  http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...

  3. Mysql Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='

    MySQL字符串比较bug: select * from table_a a left join table_b b on a.field_a = b.field_b   error: Illegal ...

  4. TNS-12535: TNS:operation timed out案例解析

    一数据库突然连接不上,在自己电脑上使用SQL Developer也连接不上.立即使用SecureCRT连接上了这台服务器,从下面几个方面检查. 1:检查了数据库的状态是否正常 $ sqlplus / ...

  5. 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted

    1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...

  6. the user operation is waiting

    eclipse在编辑完代码保存的时候,弹出一个进度框,等N长时间,标题是"user operation is waiting",里面显示的是building workspace的进 ...

  7. Python安装pywinauto时遇到error: The read operation timed out解决方法

    Python结合Pywinauto 进行 Windows UI 自动化,安装pywinauto时遇到的一些问题: 解决方法:很明显是链接超时国外网站你懂的V_P_N吧,直接通过报错信息的链接复制到浏览 ...

  8. svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法

    今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...

  9. windows下安装kibana出 "EPERM: operation not permitted

    D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...

  10. iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.

    Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...

随机推荐

  1. 《数据结构-C语言》顺序表

    @ 目录 顺序表 结构定义 初始化 创建表 求表长 判断表是否为空 取值 查找 插入 删除 逆置 清空 销毁 遍历打印 测试 顺序表 结构定义 #include <stdio.h> #in ...

  2. Jquery tableExport.js将网页中的表格导出为Excel

    需求:将如下网页中的所有表格一次导入到Excel文件中. 方法:使用jQuery的tableExport.js插件,可以将网页中指定的table表格数据导出到Excel文件,而不需要经过后台. 操作步 ...

  3. HDLbits_Conwaylife

    题目介绍 题目链接 Conwaylife 简介 题目要求我们实现一个康威生命游戏的电路. 该游戏在一个二维网格空间中进行,在该题目中是 16 * 16 的大小,每一个格子都有两种状态(0 或 1),代 ...

  4. numpy 索引,切片 ,转置,变值,多个数组的拼接

  5. SQL运用full join连接,decode函数,nvl函数进行列转行

    背景说明:full join连接,decode函数,nvl函数,结合生产背景理解更深刻: test_A表是现金业主某时间段的收益:test_B表是非现金业主某时间段的收益:对于业主,test_A表与t ...

  6. DB2---创建返回结果集的函数

    在数据验证中,经常遇到需返回结果集的操作,故整理一个返回结果集的DB2函数,便于后期查阅 1.准备测试表 /*创建测试表:设置结果集的属性为表字段*/ CREATE TABLE Test_EXWAST ...

  7. 圆角android

    资源地址 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid an ...

  8. Vocabulary

    词汇(Vocabulary) blackmail ( n.) :the obtaining of money or advancement by threatening to make known u ...

  9. 传纸条 luoguP1006

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排坐成一个 mm 行 nn 列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈 ...

  10. 关于Android Stuido2.3和Eclipse4.4

    近3年没有做Android开发了,当时用是ECLISPE电脑配置2g,用的还可以. 现在又重新开始做安卓程序,发现大家都用AS了,作为技术人员,也就开始用了. (几年前AS已经发布,不过是0.x版本, ...