Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record!

For rolling a ball one gets a score — an integer (maybe negative) number of points. Score for the i-th roll is multiplied by i and scores are summed up. So, for k rolls with scores s1, s2, ..., sk, the total score is . The total score is 0 if there were no rolls.

Limak made n rolls and got score ai for the i-th of them. He wants to maximize his total score and he came up with an interesting idea. He can say that some first rolls were only a warm-up, and that he wasn't focused during the last rolls. More formally, he can cancel any prefix and any suffix of the sequence a1, a2, ..., an. It is allowed to cancel all rolls, or to cancel none of them.

The total score is calculated as if there were only non-canceled rolls. So, the first non-canceled roll has score multiplied by 1, the second one has score multiplied by 2, and so on, till the last non-canceled roll.

What maximum total score can Limak get?

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the total number of rolls made by Limak.

The second line contains n integers a1, a2, ..., an (|ai| ≤ 107) — scores for Limak's rolls.

Output

Print the maximum possible total score after cancelling rolls.

Examples

Input
6
5 -1000 1 -3 7 -8
Output
16
Input
5
1000 1000 1001 1000 1000
Output
15003
Input
3
-60 -70 -80
Output
0

题意:给定一段数列,现在叫你取其中一段,第一位*1,第二位*2...求最大。

思路:设前缀和和是sum[n]=a[1]+a[2]+...a[n],原先的dp[n]=a[1]*1+a[2]*2+a[3]*3+..a[n]*n;

那么现在的max[i]=max:dp[i]-dp[j]-(sum[i]-sum[j])*j; =-sum[i]*j+j*sum[j]-dp[j]+dp[j]

显然可以斜率优化,由于K=sum[i]并不是单调递增的,所以我们维护凸包不能弹出队首,而需要二分。

BZOJ2726一样,就补多说了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
ll s[maxn],dp[maxn],q[maxn],top,ans;
ll Y(int i){ return s[i]*i-dp[i];}
ll g(int i,int j){ return dp[i]-dp[j]-j*(s[i]-s[j]); }
int main()
{
int N,i,j;
scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%I64d",&s[i]);
dp[i]=dp[i-]+s[i]*i;
s[i]+=s[i-];
ans=max(ans,dp[i]);
}
for(i=;i<=N;i++){
int L=,R=top-,Mid,t=top;
while(L<=R){
Mid=(L+R)>>;
if(g(i,q[Mid])>=g(i,q[Mid+])) t=Mid,R=Mid-;
else L=Mid+;//或者用斜率二分
}
ans=max(ans,g(i,q[t]));
while(top&&(Y(i)-Y(q[top]))*(q[top]-q[top-])>(Y(q[top])-Y(q[top-]))*(i-q[top])) top--;
q[++top]=i;
}
printf("%I64d\n",ans);
return ;
}

CodeForces - 660F:Bear and Bowling 4(DP+斜率优化)的更多相关文章

  1. Codeforces 660F Bear and Bowling 4 斜率优化 (看题解)

    Bear and Bowling 4 这也能斜率优化... max[ i ] = a[ i ] - a[ j ] - j * (sum[ i ] - sum[ j ])然后就能斜率优化啦, 我咋没想到 ...

  2. 【BZOJ-4518】征途 DP + 斜率优化

    4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 230  Solved: 156[Submit][Status][ ...

  3. 【BZOJ-3437】小P的牧场 DP + 斜率优化

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 705  Solved: 404[Submit][Status][Discuss ...

  4. 【BZOJ-1010】玩具装箱toy DP + 斜率优化

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][St ...

  5. 【BZOJ】1096: [ZJOI2007]仓库建设(dp+斜率优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1096 首先得到dp方程(我竟然自己都每推出了QAQ)$$d[i]=min\{d[j]+cost(j+ ...

  6. BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)

    [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...

  7. 学渣乱搞系列之dp斜率优化

    学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很 ...

  8. DP斜率优化总结

    目录 DP斜率优化总结 任务安排1 任务计划2 任务安排3 百日旅行 DP斜率优化总结 任务安排1 首先引入一道题,先\(O(N^2)\)做法:分别预处理出\(T_i,C_i\)前缀和\(t[i],c ...

  9. HDU 3507 [Print Article]DP斜率优化

    题目大意 给定一个长度为\(n(n \leqslant 500000)\)的数列,将其分割为连续的若干份,使得 $ \sum ((\sum_{i=j}^kC_i) +M) $ 最小.其中\(C_i\) ...

  10. dp斜率优化

    算法-dp斜率优化 前置知识: 凸包 斜率优化很玄学,凭空讲怎么也讲不好,所以放例题. [APIO2014]序列分割 [APIO2014]序列分割 给你一个长度为 \(n\) 的序列 \(a_1,a_ ...

随机推荐

  1. Java IDL与javaRMI

    Registry registry = LocateRegistry.getRegistry(); registry.rebind(RemoteService.name, stub); Java 平台 ...

  2. [读书笔记] learn python the hard way书中 有关powershell 的一些小问题

    ex46中,创建自己的python,  当你激活环境时 .\.venvs\lpthw\ Scripts\activate 会报一个错误 此时需要以管理员身份运行PowerShell,(当前的PS不用关 ...

  3. 图片压缩CompressUtil解析

    CompressUtil 流程图: CompressUtil 类 具体解释 public class CompressUtil { /** * 终于封装的压缩方法 * @param imgPath * ...

  4. Bootstrp--一个导航面板切换的实用例子

    <!--导航区开始--> <ul class="nav nav-tabs nav-stacked" role="tablist"> &l ...

  5. HTML5 2D平台游戏开发#4状态机

    在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...

  6. PHPExcel简易使用教程

    在企业里使用PHP进行开发,不可避免总会遇到读/写Excel的需求,遇到这种需求,一般使用PHPExcel类库进行开发. PHPExcel现在最新版本是1.8.0,最低需要PHP5.2版本,支持读取x ...

  7. 04 redis list结构及命令详解

    一:link 链表结构 lpush key value 作用: 把值插入到链接头部[右边] 注意:rpush key value 插入到左边 rpop key 作用: 返回并删除链表尾元素 rpush ...

  8. 用HttpClient模拟HTTP的GET和POST请求(转)

    本文转自:http://blog.csdn.net/xiazdong/article/details/7724349 一.HttpClient介绍   HttpClient是用来模拟HTTP请求的,其 ...

  9. 用Newtonsoft将json串转为对象的方法(详解)

    首先,将json串转为一个JObject对象: JObject jo = (JObject)JsonConvert.DeserializeObject(CurrentSelectedItemReq) ...

  10. C# xml读取操作

    以下xml: <Project> <ProjectMains> <ProjectMain Action="added"> <Project ...