E. Product Sum
 

Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.

You are given an array a of length n. The characteristic of this array is the value  — the sum of the products of the valuesai by i. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 200 000) — the size of the array a.

The second line contains n integers ai (1 ≤ i ≤ n, |ai| ≤ 1 000 000) — the elements of the array a.

Output

Print a single integer — the maximum possible value of characteristic of a that can be obtained by performing no more than one move.

Examples
input
4
4 3 2 5
output
39
Note

In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be3·1 + 2·2 + 4·3 + 5·4 = 39.

In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be1·1 + 1·2 + 1·3 + 2·4 + 7·5 = 49.

题意:

  给你一个序列a,让你求∑ a[i]*i 是多少

  你可以进行一次操作:将任意位置的一个数组元素拿出来再插入任意一个新的位置或者不进行此操作。

  问你最大的∑ a[i]*i 是多少。

题解:

  首先假设拿出元素向前面的位置插入

  那么 dp[i] = max(pre[i-1]+i*a[i],pre[i-1]+sum[i-1]-sum[j-1] +j*a[i]);

  pre表示前缀答案和,sum表示数组前缀和,这个转移方程是可以用斜率优化的,只不过斜率并不满足单调性质,那么我们就要手动维护一个凸包来二分找答案了。。。

  拿元素向后插是一样的道理

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e3+, mod = 1e9+, inf = 2e9+; LL dp1[N],dp2[N],sum[N],las[N],pre[N];
LL splopex(int i,int j) {
return sum[i-] - sum[j-];
}
LL splopey(int i,int j) {
return i - j;
}
int n,a[N],q[N];
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) sum[i] = sum[i-] + a[i];
int head = , tail = ;
las[] = ;
for(int i = ; i <= n; ++i) {
dp1[i] = las[i-] + 1LL * i * a[i];
las[i] = dp1[i];
if(head == tail) {q[tail++] = i;continue;}
int l = head, r = tail-,md;
while( l < r ) {
md = (l+r)>>;
if(md + < tail && splopex(q[md+],q[md]) < 1LL*splopey(q[md+],q[md])*a[i])
l = md + ;
else
r = md;
}
md = r;
dp1[i] = max(las[i-] + 1LL * sum[i-] - 1LL * sum[q[md]-] + 1LL*q[md]*(a[i]),dp1[i]);
while(head + <tail && splopey(i,q[tail-])*splopex(q[tail-],q[tail-])
>= splopey(q[tail-],q[tail-])*splopex(i,q[tail-])) tail--;
q[tail++] = i;
} pre[n+] = ;
head = , tail = ;
for(int i = n; i >= ; --i) {
dp2[i] = pre[i+] + 1LL * i * a[i];
pre[i] = dp2[i];
if(head == tail) {q[tail++] = i;continue;}
int l = head, r = tail-,md;
while( l < r ) {
md = (l+r)>>;
if(md + < tail && splopex(q[md+]+,q[md]+) < 1LL*splopey(q[md+],q[md])*a[i])
l = md + ;
else
r = md;
}
md = r;
dp2[i] = max(pre[i+] - 1LL * sum[q[md]] + 1LL * sum[i] + 1LL*q[md]*(a[i]),dp2[i]);
while(head + <tail && splopey(i,q[tail-])*splopex(q[tail-]+,q[tail-]+)
<= splopey(q[tail-],q[tail-])*splopex(i+,q[tail-]+)) tail--;
q[tail++] = i;
}
LL ans = -INF;
for(int i = ; i <= n; ++i) {
ans = max(ans, max(dp1[i]+pre[i+],dp2[i]+las[i-]));
}
cout<<ans<<endl;
return ;
}

Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP的更多相关文章

  1. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  2. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  3. Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)

    题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...

  4. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  5. Codeforces Round #344 (Div. 2)

    水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; sc ...

  6. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  7. Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)

    Imagine that you are in a building that has exactly n floors. You can move between the floors in a l ...

  8. Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  9. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

随机推荐

  1. 修改mysql数据默认存储路径

    1:停止mysql服务 2:找到配置文件路径 C:\ProgramData\MySQL\MySQL Server 5.6\my.ini 3:修改属性datadir 1.将C:/ProgramData/ ...

  2. Linux从入门到适应(三):Ubuntu16.04将python从3.5升级到3.6

    1 将python从默认的python2.7更换为python3.5 : sudo update-alternatives --install /usr/bin/python python /usr/ ...

  3. Python re模块 subprocess模块

    re模块 内部实现不是Python 而是调用了c的库 re是什么 正则 表达 式子 就是一些带有特殊含义的符号或者符号的组合作用: 对字符串进行过滤 在一对字符串中找到所关心的内容 你就需要告诉计算机 ...

  4. Shiro框架 (原理分析与简单实现)

    Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理   (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...

  5. tcp案例之文件下载器

    文件下载器客户端 import socket def main(): # 1.创建一个tcp socket tcp_client_socket=socket.socket(socket.AF_INET ...

  6. Go:struct

    一.使用方式 方式3和方式4返回的是:结构体指针,编译器底层对 p.Name 做了转化 *(p).Name,方便程序员使用. type Person struct { Name string Age ...

  7. jmeter 接口测试

    web接口测试工具: 手工测试的话可以用postman ,自动化测试多是用到 Jmeter(开源).soupUI(开源&商业版). 下面将对前一篇Postman做接口测试中的接口用Jmeter ...

  8. ppt_旋转抽奖_制作步骤

    1 ppt制作抽奖转盘 插入饼状图,更改比例,更改颜色(一般选取相邻的一组颜色匹配比较好看): 插入竖文本框,编辑每一个部分为特等奖.一等奖.二等奖.三等奖: 全部选中,ctrl+G组合: 添加陀螺旋 ...

  9. Windows Server 2008 R2 Enterprise 安装.NET Framework 4.0

    由于服务器上没有.NET 4.0 部署不了 4.0及以上版本的网站,所以给他安排一下:​ 复制下好的.NET Framework 4.0到服务器开始安装: ​ ​ ​ ​ 安装完,重新打开IIS,已经 ...

  10. 【Codeforces 1086B】Minimum Diameter Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...