Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value ai.
First, Alice chooses a non-empty consecutive segment of cards [l;r] (l≤r). After that Bob removes a single card j from that segment (l≤j≤r). The score of the game is the total value of the remaining cards on the segment (al+al+1+⋯+aj−1+aj+1+⋯+ar−1+ar). In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is 0.
Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.
What segment should Alice choose so that the score is maximum possible? Output the maximum score.
Input
The first line contains a single integer n (1≤n≤105) — the number of cards.
The second line contains n integers a1,a2,…,an (−30≤ai≤30) — the values on the cards.
Output
Print a single integer — the final score of the game.
Examples
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
Note
In the first example Alice chooses a segment [1;5] — the entire row of cards. Bob removes card 3 with the value 10 from the segment. Thus, the final score is 5+(−2)+(−1)+4=6.
In the second example Alice chooses a segment [1;4], so that Bob removes either card 1 or 3 with the value 5, making the answer 5+2+3=10.
In the third example Alice can choose any of the segments of length 1: [1;1], [2;2] or [3;3]. Bob removes the only card, so the score is 0. If Alice chooses some other segment then the answer will be less than 0.
思路:根据题意,我们可以想得到的是Bob肯定设法减去的是Alice选取区间的最大值,这样看来,我们就可以枚举每一个位置的数,看看它在哪一段区间是最大值。这就用到了单调栈。对于第i个数字来说,它的左右区间分别是[L,R].那么我们就求[L,i-1]的最大值和[i+1,R]的最大值(如果有负数就取0),然后相加得到最大值。
线段树中,我们存储的是前缀和与后缀和,这样就需要开两棵线段树。然后更新的时候需要分前缀还是后缀这样讨论。具体看代码吧。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e5+100;
struct node{
int l,r,lazy,_max;
}p1[maxx<<2],p2[maxx<<2];
int a[maxx];
int L[maxx],R[maxx],suml[maxx],sumr[maxx];
int n;
inline void pushup(int cur,node p[])
{
p[cur]._max=max(p[cur<<1]._max,p[cur<<1|1]._max);
}
inline void pushdown(int cur,node p[])
{
if(p[cur].lazy)
{
p[cur<<1]._max+=p[cur].lazy;
p[cur<<1|1]._max+=p[cur].lazy;
p[cur<<1].lazy+=p[cur].lazy;
p[cur<<1|1].lazy+=p[cur].lazy;
p[cur].lazy=0;
}
}
inline void build(int l,int r,int cur,node p[])
{
p[cur].l=l;
p[cur].r=r;
p[cur].lazy=p[cur]._max=0;
if(l==r) return ;
int mid=l+r>>1;
build(l,mid,cur<<1,p);
build(mid+1,r,cur<<1|1,p);
}
inline void update(int l,int r,int cur,node p[],int v)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r)
{
p[cur].lazy+=v;
p[cur]._max+=v;
return ;
}
pushdown(cur,p);
int mid=L+R>>1;
if(r<=mid) update(l,r,cur<<1,p,v);
else if(l>mid) update(l,r,cur<<1|1,p,v);
else update(l,mid,cur<<1,p,v),update(mid+1,r,cur<<1|1,p,v);
pushup(cur,p);
}
inline int query(int l,int r,int cur,node p[])
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r) return p[cur]._max;
pushdown(cur,p);
int mid=L+R>>1;
if(r<=mid) return query(l,r,cur<<1,p);
else if(l>mid) return query(l,r,cur<<1|1,p);
else return max(query(l,mid,cur<<1,p),query(mid+1,r,cur<<1|1,p));
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
stack<int> s;
while(s.size()) s.pop();
for(int i=1;i<=n;i++)
{
while(s.size()&&a[s.top()]<=a[i]) s.pop();
if(s.size()==0) L[i]=1;
else L[i]=s.top()+1;
s.push(i);
}
while(s.size()) s.pop();
for(int i=n;i>=1;i--)
{
while(s.size()&&a[s.top()]<=a[i]) s.pop();
if(s.size()==0) R[i]=n;
else R[i]=s.top()-1;
s.push(i);
}
suml[0]=sumr[n+1]=0;
for(int i=1;i<=n;i++) suml[i]=suml[i-1]+a[i];
for(int i=n;i>=1;i--) sumr[i]=sumr[i+1]+a[i];
build(1,n,1,p1);
build(1,n,1,p2);
for(int i=1;i<=n;i++) update(i,i,1,p1,suml[i]);
for(int i=n;i>=1;i--) update(i,i,1,p2,sumr[i]);
int l,r,ans1,ans2,ans=0;
for(int i=1;i<=n;i++)
{
l=L[i];
r=R[i];
ans1=ans2=0;
if(i+1<=r)
{
update(i+1,r,1,p1,-suml[i]);
ans1=max(query(i+1,r,1,p1),ans1);
update(i+1,r,1,p1,suml[i]);
}
if(i-1>=l)
{
update(l,i-1,1,p2,-sumr[i]);
ans2=max(query(l,i-1,1,p2),ans2);
update(l,i-1,1,p2,sumr[i]);
}
ans=max(ans,ans1+ans2);
}
printf("%d\n",ans);
return 0;
}
有什么别的好思路希望路过的大佬指点。
努力加油a啊,(o)/~