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. 【C语言】控制台窗口图形界面编程(二)窗口信息和填充缓冲区

    目录 00. 目录 01. COORD结构体 02. SMALL_RECT结构 03. CONSOLE_SCREEN_BUFFER_INFO结构体 04. GetConsoleScreenBuffer ...

  2. P2P实现的原理

    为了项目的后期IM应用,最近在研究libjingle,中间看了也收集了很多资料,感慨网上很多资料要么太过于纠结协议(如STUN.ICE等)实现细节,要么中间有很多纰漏.最后去伪存真,归纳总结了一下,希 ...

  3. ajax 传参数 数组 会加上中括号

    解决办法 1,由于版本过高导致 我用的是1.9版本 2, 有三种选择. 一种是JSON.stringify([1,2,3]),到后端再解析. 另外一种是后端的接受的contentType改成appli ...

  4. js读取文本内容,支持csv.txt

    js读取文本内容,支持csv.txt <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  5. kafka 服务端消费者和生产者的配置

    在kafka的安装目录下,config目录下有个名字叫做producer.properties的配置文件 #指定kafka节点列表,用于获取metadata,不必全部指定 #需要kafka的服务器地址 ...

  6. [JOYOI] 1055 沙子合并

    题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 设有N堆沙子排成一排,其编号为1,2,3,-,N(N<=300).每堆沙子有 ...

  7. 树莓派 -- 输入设备驱动 (key) 续2: 转载 Setting up a GPIO-Button “keyboard” on a Raspberry Pi

    使用device-tree (DT) overlay应该是更方便的方法: http://blog.gegg.us/2017/01/setting-up-a-gpio-button-keyboard-o ...

  8. selenium--driver.switchTo()-----转

    https://www.cnblogs.com/clairejing/p/9499223.html 在自动化测试中,会遇到多窗口.多iframe.多alert的情况.此时,会使用driver.swit ...

  9. Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)

    题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...

  10. Vue如何实现swiper左右滑动内容区控制导航tab同时切换高亮

    Vue如何实现左右滑动内容区控制导航tab同时切换高亮,实现的效果是:点击导航按钮时内容区发生改变,左右滑动内容区时导航按钮跟随切换高亮,停留在某个内容区时刷新页面后仍然停留在当前内容区.       ...