10
-3 4 -4 1 1 -4 -5 3 -4 -2
 10
8 9 -10 -10 -4 -3 5 9 -9 9
 6
5 -4 2 -1 2 -2
 11
3 0 1 -2 5 -5 -1 0 3 2 2
 10
4 -2 -2 -2 2 0 1 0 -4 -1
 4
2 2 -3 4
 11
-11 -7 -2 -24 30 -26 24 9 -8 -9 23
 6
30 -29 5 5 -29 30
 24
-8 -6 6 -11 -15 -7 10 -19 -6 21 14 -4 -21 -10 12 17 1 -1 5 19 2 0 -4 -23
 10
-9 -6 -1 9 -2 0 0 -1 5 0
 6
10 -8 3 3 -8 10
 10
-2 -3 -1 4 -3 2 0 1 -5 4
 5
30 -29 10 -1 10
 31
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -29 30
 7
30 -20 5 1 3 -20 30
 5
30 -29 4 -3 4
 10
-5 28 -18 -10 9 -2 11 -6 -19 30
 8
4 -10 13 -13 2 2 -11 6
 131
30 -29 29 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 27 -26 27
 10
-4 4 0 -2 3 0 1 2 3 -4
 10
-2 -8 5 4 -1 -6 -9 6 1 -6
 7
25 -17 13 13 -25 25 -24
 10
3 -3 2 1 -4 -2 3 -2 -3 0
 10
4 -5 0 4 0 0 -3 1 1 -4
 10
0 4 4 1 -3 4 2 1 4 -1
 17
15 -13 7 8 8 1 -19 12 8 -23 -4 16 -13 -28 10 -30 -14
 4
1 1 -4 10
 11
3 -2 1 1 1 1 1 1 1 -3 4
 7
30 -29 10 10 10 -29 30
 7
25 -13 17 -23 12 12 9
 7
15 -14 10 10 10 -14 15
 10
3 0 0 -4 3 1 -2 -1 -1 4
 6
8 -7 7 -6 5 5
 10
1 -4 -4 4 -2 -3 3 0 -2 3
 15
1 -26 -6 26 7 10 -2 -4 -6 14 -9 25 9 -12 -28
 6
30 -29 10 10 -29 30
 1
6
 2
1 1
 5
0 5 -4 3 -1
 7
5 -4 3 -1 3 -4 4
 10
16 -15 13 -1 10 -2 -20 26 5 -30
 10
-1 2 0 -1 2 -1 4 3 -5 1
 7
5 -4 1 1 1 -4 5
 5
10 -3 -5 3 5
 10
4 -4 3 -2 3 -4 -2 0 1 0
 10
-5 -5 -3 1 -2 2 0 2 -3 4
 10
2 -4 -2 3 1 1 -2 -1 -3 4
 10
-4 -5 4 -3 2 -2 2 1 -1 0
 10
0 -1 4 -2 3 -2 -1 -5 -5 2
 5
19 -6 1 -11 19
 10
1 0 -4 3 -5 3 -1 2 2 -5
 7
5 -5 3 4 3 -5 5
 10
-5 2 -3 -3 -4 3 3 0 2 4
 4
2 3 -10 11
 10
27 -17 15 -22 18 17 -6 2 16 -14
 6
30 -22 6 7 -22 30
 7
30 -25 0 6 9 -2 10
 9
7 2 -1 1 1 -10 6 -1 6
 4
1 -10 1 10
 5
17 -16 12 -11 15
 35
-23 -22 23 4 -22 29 -4 -6 -28 18 -5 21 -7 -8 -11 -7 30 -25 -1 12 19 5 -8 12 -1 10 -24 -19 24 -17 -7 24 20 -22 16
 10
4 -3 1 -2 1 1 0 -4 4 -2
 10
4 -2 1 0 1 -5 -2 1 0 -5
 2
3 9
 9
11 -5 -5 1 2 3 -5 -5 11

1
 8
 1
 4
 1
 2
 15
 5
 36
 2
 3
 1
 9
 28
 4
 1
 7
 2
 1
 7
 4
 13
 1
 1
 13
 16
 1
 6
 20
 21
 20
 1
 5
 1
 44
 10
 0
 1
 0
 2
 9
 5
 2
 3
 1
 2
 2
 1
 1
 3
 3
 6
 8
 2
 29
 6
 13
 5
 1
 1
 30
 1
 1
 3
 3

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
#include <iostream> 
using namespace __gnu_pbds; 
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag, 
             tree_order_statistics_node_update> 
    oset; 
   
#define md 1000000007
#define itn int
#define pb push_back
#define mp make_pair
#define ull unsigned long long 
#define ll long long 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define x first    
#define lb lower_bound
#define ub upper_bound
#define y second
#define sz(x) (int)x.size()
#define inf 1e9
#define endl '\n'
#define cuot cout
#define dbs long double
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define trace(v) for(auto it=v.begin();it!=v.end();it++)cerr<<*it<<" ";cerr<<endl;
#define linf 1e18
#define reset(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(int i=a;i<b;i++)
#define eps 1e-12
#define randomize srand(chrono::steady_clock::now().time_since_epoch().count());

void fastio(){    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

ll n;
ll ps[2][200000];

ll lookup[2][200000][100]; 

ll query(ll l,ll r,ll c){
    if(l>r)return 0;
    ll j = log2(r-l+1);
    // trace4(l,r,c,max(lookup[c][l][j],lookup[c][r-(1<<j)+1][j]));
    return max(lookup[c][l][j],lookup[c][r-(1<<j)+1][j]);
}

ll go(ll l,ll m,ll r){
    ll res = 0;
    if(l+1<=m-1){
        res += query(l+1,m-1,1) - ps[1][m];
    }
    if(m+1<=r-1)
        res += query(m+1,r-1,0) - ps[0][m];
    return res;
}
void solve(){
    cin>>n;
    vector<ll> v(n);
    vector<ll> arr[100];
    ll sm = 0;
    ll mx = -31;
    rep(i,0,n){
        cin>>v[i];
        sm+=v[i];
        ps[0][i] = sm;
        // arr.pb(mp(v[i],i));
        arr[30+v[i]].pb(i);
    }
    sm = 0;
    
    for(int i=n-1;i>=0;i--){
        sm+=v[i];
        ps[1][i] = sm;
    }
    ll J = log2(n)+1;
    for(int k=0;k<2;k++){
        for(int i=0;i<n;i++){
            lookup[k][i][0] = ps[k][i];
        }
        for(int j=1;j<=J;j++){
            for(int i=0;i+(1<<j)<=n;i++){
                lookup[k][i][j] = max(lookup[k][i][j-1],lookup[k][i+(1<<(j-1))][j-1]);
            }
        }
    }

    ll ans = 0;
    oset os;
    os.insert(-1);
    os.insert(n);

    for(int i=60;i>=0;i--){
        for(auto x:arr[i]){
            ll l = os.order_of_key(x);
            ll r = *os.find_by_order(l);
            l = *os.find_by_order(l-1);
            ans = max(ans,go(l,x,r));
            // trace4(x,l,r,ans);
        }
        for(auto x:arr[i]){
            os.insert(x);
        }
    }
    cout<<ans<<endl;
}
 
int main(){
    fastio();
    // #ifndef ONLINE_JUDGE 
  
    // // For getting input from input.txt file 
    // freopen("in.txt", "r", stdin); 
  
    // // Printing the Output to output.txt file 
    // freopen("out.txt", "w", stdout); 
  
    // #endif 
    int t;    
    t=1;
    // cin>>t;
    for(int i=1;i<=t;i++){    
        // cout<<"Case #"<<i<<": ";
        solve();
    }    
    return 0;
}    