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. bzoj4010【HNOI2015】菜肴制作

    4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec  Memory Limit: 512 MB Submit: 981  Solved: 480 [Submit][Statu ...

  2. 2014年7月微软MVP名单揭晓!

    微软公司于2001年8月起開始在亚洲与各大基本的第三方站点上的微软技术相关论坛合作,微软称之为"亚洲社区支持"计划.    为了鼓舞大家在论坛中更好地互相帮助,共同提高,微软在全亚 ...

  3. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  4. C#中的let字句应用示例

    一.应用场景 在查询表达式中,存储子表达式的结果有时很有用,这样可以在随后的子句中使用. 可以使用 let 关键字完成这一工作,该关键字可以创建一个新的范围变量,并且用您提供的表达式的结果初始化该变量 ...

  5. 成长型思维模式Not yet

    当做一件事失败了,要告诉他 暂未成功,暂时没有成功,继续努力,下次就有可能成功. 不及格,暂未及格  是两种思维模式,成长性的思维方式得到的是一个是努力型的人格

  6. 大白第一章第四节dp例题

    入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...

  7. iOS怎样获取任何App的资源图片?

    1.打开iTunes,并与手机相连接 2.按照下图所示执行搜索并下载App 3.到Mac的 /Users/apple/Music/iTunes/iTunes Media/Mobile Applicat ...

  8. 关于String,StringBuffer与StringBuilder的区别

    String是字符串常量对象,对其进行改变时会相当影响效率,特别注意在循环中直接拼接字符串效率非常差. 如果你想改变字符串的值,更加推荐使用StringBuffer与StringBuilder两种可变 ...

  9. JDK动态proxy原理解析

    转: 之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白.比如说:InvocationHandler的invoke方法是由谁来调用的,代理对象是怎么生成的,直到前几个星期才把这些问题全部搞明白 ...

  10. matlab使用usb和gige 网口相机

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...