D. Yet Another Yet Another Task
Alice and Bob are playing yet another card game. This time the rules are the following. There are nn cards lying in a row in front of them. The ii-th card has value aiai.
First, Alice chooses a non-empty consecutive segment of cards [l;r][l;r] (l≤rl≤r). After that Bob removes a single card jj from that segment (l≤j≤r)(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)(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 00.
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 nn (1≤n≤1051≤n≤105) — the number of cards.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−30≤ai≤30−30≤ai≤30) — the values on the cards.
Output
Print a single integer — the final score of the game.
Examples
input
Copy
5 5 -2 10 -1 4output
Copy
6input
Copy
8 5 2 5 3 -30 -30 6 9output
Copy
10input
Copy
3 -10 6 -15output
Copy
0Note
In the first example Alice chooses a segment [1;5][1;5] — the entire row of cards. Bob removes card 33 with the value 1010 from the segment. Thus, the final score is 5+(−2)+(−1)+4=65+(−2)+(−1)+4=6.
In the second example Alice chooses a segment [1;4][1;4], so that Bob removes either card 11 or 33 with the value 55, making the answer 5+2+3=105+2+3=10.
In the third example Alice can choose any of the segments of length 11: [1;1][1;1], [2;2][2;2] or [3;3][3;3]. Bob removes the only card, so the score is 00. If Alice chooses some other segment then the answer will be less than 00.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define pi acos(-1)
#define ull unsigned long long
#define ll long long
#define pb push_back
#define all(vc) vc.begin() , vc.end()
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc now<<1
#define rc now<<1|1
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 1e6 +7;
const int inf = 1e9;
int _,n,m,t,k,u,v,w,ans,cnt,ok,lim,len,tmp,last,nx;
struct node {int u,v,nx,w;}e[mxn];
string str;
int a[mxn];
void solve()
{
ans = 0 ;
for(int i=1;i<=30;i++)
{
int nowans = 0 , nowmin = 0 ;
for(int j=1;j<=n;j++)
{
if(a[j]>i){
nowans = 0 , nowmin = 0 ; continue;
}
nowans += a[j] ;
nowmin = min( nowmin , nowans );
ans = max(nowans-nowmin-i , ans);
}
}
cout<<ans<<endl;
}
int main()
{
tle;
while(cin>>n)
{
for(int i=1;i<=n;i++) cin>>a[i];
solve();
}
}