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. sqlserver 创建索引 物化 视图

    索引视图: create view  Test WITH SCHEMABINDING as select Id, Name from [dbo].[InterfaceCallSetting]creat ...

  2. jquery toggle()设置

    很多朋友对jquery toggle()比较熟练,甚至经常用到,而且对toggle的三个参数也比较了解$(selector).toggle(speed,callback,switch).但是当你设置$ ...

  3. Python re模块 subprocess模块

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

  4. Go:slice

    一.切片创建方式 func main() { // 创建切片方式1 // 让切片引用一个数组 array := [...]int{1, 2, 3, 4} slice1 := array[1:3] fm ...

  5. CIFAR100与VGG13实战

    目录 CIFAR100 13 Layers cafar100_train CIFAR100 13 Layers cafar100_train import tensorflow as tf from ...

  6. python_字符串常用方法

    1.切片就是通过索引(索引:索引:步长)截取字符串的一段,形成新的字符串(原则就是顾头不顾腚). a = 'ABCDEFGHIJK' print(a[0:3]) # print(a[:3]) 从开头开 ...

  7. poj 3253 Fence Repair (优先队列,哈弗曼)

    题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...

  8. selenium IDE脚本编辑与操作

    1.编辑一行命令 在Table标签下选中某一行命令,命令由command.Target.value三部分组成.可以对这三部分内容那进行编辑. 然后右击这行命令,选择“Execute this comm ...

  9. Java Class 利用classpath来获取源文件地址

    利用classpath来获取源文件地址 @author ixenos 应用场景 Properties props = new Properties(); /** * . 代表java命令运行的目录 * ...

  10. 洛谷 P2285 BZOJ 1207 [HNOI2004]打鼹鼠

    题目描述 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气. ...