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>
using namespace std;
// #include <ext/rope> //header with rope
// using namespace __gnu_cxx;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

/// Typedef
typedef long double ld;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
typedef vector< pii > vii;

#define pb                  push_back
#define ppb                 pop_back
#define MP                  make_pair
#define ff                  first
#define ss                  second
#define sf                  scanf
#define pf                  printf
#define FOR(i, x, y)        for(int i=int(x); i<int(y); i++)
#define ROF(i, x, y)        for(int i=int(x)-1; i>=int(y); i--)
#define Binary(x,b)         std::bitset< b >( x ).to_string()
#define fastIO              ios::sync_with_stdio(false);    cin.tie(nullptr);   cout.tie(nullptr)
#define ran()               (((((1ULL*rand())<<31)|(1ULL*rand()))<<13)^((((1ULL*rand())<<32)|(1ULL*rand()))))
#define rran(a, b)          ((ran() % ((b) - (a) + 1)) + (a))
#define CLR(x, y)           memset(x, y, sizeof(x))
#define sc(n)               sf("%d",&n)
#define sc2(a,b)            sf("%d%d",&a,&b)
#define sc3(a,b,c)          sf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)        sf("%d%d%d%d",&a,&b,&c,&d)
#define scll(n)             sf("%lld",&n)
#define scll2(a,b)          sf("%lld%lld",&a,&b)
#define scll3(a,b,c)        sf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)      sf("%lld%lld%lld%lld",&a,&b,&c,&d)
#define bitCheck(N,in)      ((bool)(N&(1LL<<(in))))
#define bitOff(N,in)        (N&(~(1LL<<(in))))
#define bitOn(N,in)         (N|(1LL<<(in)))
#define bitFlip(a,k)        (a^(1LL<<(k)))
// #define bitCount(a)         __builtin_popcount(a)
// #define bitCountLL(a)       __builtin_popcountll(a)
// #define bitRightMost(a)     __builtin_ctzll(a)
// #define bitLeftMost(a)      (63-__builtin_clzll(a))
#define SQR(x)              ((x)*(x))
#define mxEq(a,b)           (a = max(a,b))
#define all(c)              c.begin(), c.end()
#define unq(v)              sort(all(v)), (v).resize(unique(all(v)) - v.begin())
#define common(a,b)         sort(all(a)), sort(all(b)), a.erase(set_intersection(all(a),all(b),a.begin()),a.end())
#define uncommon(a,b)       sort(all(a)), sort(all(b)), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end())

// uniform random generator -->
// uniform_int_distribution<int> dist(0, 1);
// default_random_engine gen;

// *X.find_by_order(k) //returns the kth largest element.(0-based)
// X.order_of_key(val) //returns the no. of values less than val
// template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // C++98
template <class T> inline T bigMod(T p,T e,T M){ T ret = 1; for(; e > 0; e >>= 1){ if(e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T) ret;}
template <class T> inline T modInverse(T a,T M){return bigMod(a,M-2,M);}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
template <class T> inline string int2String(T a){ostringstream str; str<<a; return str.str();}
const int dr[] = { 0,  1,  0, -1,        -1,  1,  1, -1,     -2, -2,  2,  2, -1, -1,  1,  1};
const int dc[] = { 1,  0, -1,  0,         1,  1, -1, -1,     -1,  1, -1,  1, -2,  2, -2,  2};


/// Constants
#define MOD                 (int)(1e9+7)
#define eps                 1e-9
#define INF                 0x3f3f3f3f3f3f3f3f
#define inf                 0x3f3f3f3f // CLR(63)
#define PI                  (2.0*acos(0.0))  // 3.1415926535897932
#define Log(b,x)            (log(x)/log(b))

/// Debug
#define out(a,T,str)        copy(a.begin(),a.end()-1,ostream_iterator<T>(cout,str)),cout<<a.back()<<endl
#ifdef INCOGNITO
    #include <E:/myUtility.cpp>
    #define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" | ";
        __f(comma+1, args...);
    }
#else
    #define dbg(...)
#endif

/// Priority Queue custom compare
// bool cmp(const int &a, const int &b){ return a > b;}
// std::priority_queue<int, std::vector<int>, decltype(&cmp) > pq(cmp);

const int maxn = 1e5+500, 
          block_size = sqrt(maxn)+1,
          blocks = (maxn/block_size)+1;

namespace SQRT_Decomposition {
    vector<pii> b_array[blocks+10],
                b_LeftSum[blocks+10], 
                b_RightSum[blocks+10];

    vector<int> b_array_RightMax[blocks+10],
                b_array_LeftMin[blocks+10];

    int N, 
        a[maxn], 
        LeftSum[maxn], 
        RightSum[maxn];

    inline void preprocess(){
        FOR(i,0,N){
            b_array[ i/blocks].pb(MP(a[i], i));
            b_LeftSum[ i/blocks].pb(MP(LeftSum[i], i));
            b_RightSum[ i/blocks].pb(MP(RightSum[i], -i));
        }
        FOR(i,0,blocks){
            sort(all(b_array[i]));
            sort(all(b_LeftSum[i]));
            sort(all(b_RightSum[i]));


            for(const pii &p: b_array[i]){
                b_array_RightMax[i].pb(p.ss);
            }
            b_array_LeftMin[i] = b_array_RightMax[i];

            ROF(j, b_array_RightMax[i].size()-1, 0){
                b_array_RightMax[i][j] = max(b_array_RightMax[i][j], b_array_RightMax[i][j+1]);
            }
            FOR(j, 1, b_array_LeftMin[i].size()){
                b_array_LeftMin[i][j] = min(b_array_LeftMin[i][j], b_array_LeftMin[i][j-1]);
            }
        }   
    }
    
    inline int query_left(const int &Idx){

        int block_No = Idx/block_size;
        for(int i = Idx-1; i >= block_No*block_size; i--){
            if(a[i] > a[Idx]) return i;
        }        

        for(int i = block_No-1; i >= 0; i--){
            int j = lower_bound(all(b_array[i]), MP(a[Idx]+1,inf)) - b_array[i].begin();
            if(j < b_array[i].size()){
                return b_array_RightMax[i][j];
            }
        }

        return -1;
    }

    inline int query_right(const int &Idx){

        int block_No = Idx/block_size;
        for(int i = Idx+1; i < (block_No+1)*block_size; i++){
            if(a[i] > a[Idx]) return i;
        }        

        for(int i = block_No+1; b_array[i].size(); i++){
            int j = lower_bound(all(b_array[i]), MP(a[Idx]+1,-inf)) - b_array[i].begin();
            if(j < b_array[i].size()){
                return b_array_LeftMin[i][j];
            }
        }

        return N;
    }

    inline int leftSumMax(const int &Limit, const int &Idx){
        pii mx = MP(LeftSum[Idx], Idx);

        int block_No = Idx/block_size;
        int L_block_No = Limit/block_size;
        
        if(L_block_No == block_No){
            for(int i = Idx; i < Limit; i++){
                mx = max(mx, MP(LeftSum[i], i));
            }  
            return mx.ss;
        }

        for(int i = Idx; i < (block_No+1)*block_size; i++){
            mx = max(mx, MP(LeftSum[i], i));
        }   

        for(int i = block_No+1; i < L_block_No; i++){
            mx = max(mx, b_LeftSum[i].back());
        }

        for(int i = L_block_No*block_size; i < Limit; i++){
            mx = max(mx, MP(LeftSum[i], i));
        } 

        return mx.ss;
    }
    inline int RightSumMax(const int &Limit, const int &Idx){
        pii mx = MP(RightSum[Idx], -Idx);

        int block_No = Idx/block_size;
        int L_block_No = Limit/block_size;
        
        if(L_block_No == block_No){
            for(int i = Limit+1; i < Idx; i++){
                mx = max(mx, MP(RightSum[i], -i));
            }  
            return -mx.ss;
        }

        for(int i = block_No*block_size; i < Idx; i++){
            mx = max(mx, MP(RightSum[i], i));
        }   

        for(int i = L_block_No+1; i < block_No; i++){
            mx = max(mx, b_RightSum[i].back());
        }

        for(int i = Limit+1; i < (L_block_No+1)*block_size; i++){
            mx = max(mx, MP(RightSum[i], i));
        } 

        return -mx.ss;
    }
}
using namespace SQRT_Decomposition;

int main(){
    #ifdef INCOGNITO
        int start_s=clock();
        freopen("E:\\input.txt","r",stdin);
        freopen("E:\\output.txt","w",stdout);
    #endif      
    // fastIO;
    // srand((rand()-time(NULL))%MOD);

    int n; sc(n);
    N = n;

    FOR(i,0,n) sc(a[i]);
    
    LeftSum[0] = a[0];
    FOR(i,1,n) LeftSum[i] = LeftSum[i-1] + a[i];

    RightSum[n-1] = a[n-1];
    ROF(i,n-1,0) RightSum[i] = RightSum[i+1] + a[i];

    // FOR(i,0,n){
    //     pf("%3d %3d\n", LeftSum[i], RightSum[i]);
    // }
 
    preprocess();

    int ans = 0;
    FOR(i,0,n){
         // i = 9;

        int lBig = query_left(i);
        int rBig = query_right(i);      
        // dbg(lBig, rBig);
        
        int mxLsum = leftSumMax(rBig, i);
        int mxRsum = RightSumMax(lBig, i);

        int l = mxRsum;
        int r = mxLsum;
        // dbg(l,r, i);

        int sum = LeftSum[r]-(l ? LeftSum[l-1] : 0);
        // dbg(sum, sum-a[i]);

        // cout << endl;
        // break;

        ans = max(ans, sum-a[i]);
    }

    pf("%d\n", ans);


    #ifdef INCOGNITO
        int end_s=clock();    
        printf("\nTime :: %.2lf\n", (double)(end_s-start_s)/CLOCKS_PER_SEC);
    #endif
    return 0;
}
