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>

#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);

#define ll long long
#define ld long double

#define pb push_back
#define ins insert
#define er erase

#define f(i, a, b) for (int i = a; i < b; ++i)
#define rf(i, a, b) for (int i = a; i >= b; --i)
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define mem(a, b) memset(a, b, sizeof(a))

#define vi vector<int>
#define vst vector<string>
#define vpii vector<pii>

#define all(v) v.begin(), v.end()
#define lb lower_bound
#define ub upper_bound
#define mxe max_element
#define mne min_element

#define srt(v) sort(all(v))
#define rsrt(v) sort(v.rbegin(), v.rend())

#define pii pair<int, int>
#define ff first
#define ss second

#define bs bitset
#define bpc(a) __builtin_popcount(a)

#define mii map<int, int>
#define umii unordered_map<int, int>

#define si set<int>
#define usi unordered_set<int>
#define spii set<pii>

#define pqi priority_queue<int>
#define pqpii priority_queue<pii>

#define dbg(a, st, end)                   \
    f(i, st, end + 1) cerr << a[i] << sp; \
    cerr << nl;
#define pnt(a, st, end)                   \
    f(i, st, end + 1) cout << a[i] << sp; \
    cout << nl;
#define dbgc(a)          \
    for (auto y : a)     \
        cerr << y << sp; \
    cerr << nl;
#define dbgm(a)                            \
    for (auto y : a)                       \
        cerr << y.ff << "," << y.ss << sp; \
    cerr << nl;

#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a)*1ll * (b)) / gcd(a, b)

#define nl "\n"
#define sp " "

using namespace std;
using namespace std::chrono;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update>
#define ok order_of_key
#define fo find_by_order

ll md = 1000000007;
ld pi = 3.141592653;
//ll md = 998244353;

ll pw(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m);
        m = (m * m);
        b /= 2;
    }
    return c;
}

ll pwmd(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}

ll modinv(ll n)
{
    return pwmd(n, md - 2);
}

ll my_ceil(ll a, ll b)
{
    if (a % b == 0)
        return a / b;
    return a / b + 1;
}

ll nc2(ll n)
{
    return (1ll * n * (n - 1)) / 2;
}

ll nsum(ll n)
{
    return (1ll * n * (n + 1)) / 2;
}

int my_log(ll n, int b)
{
    ll i = 1;
    int ans = 0;
    while (1)
    {
        if (i > n / b)
            break;
        i *= b;
        ans++;
    }
    return ans;
}

inline int nc()
{
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline int read()
{
    int ret = 0;
    bool f = 0;
    char ch = nc();
    while (ch > '9' || ch < '0')
        f ^= ch == '-', ch = nc();
    while (ch <= '9' && ch >= '0')
        ret = ret * 10 + ch - '0', ch = nc();
    return f ? -ret : ret;
}

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

inline ll add(ll a, ll b) { return (((a >= md) ? a % md : a) + ((b >= md) ? b % md : b) + md) % md; }
inline ll subt(ll a, ll b) { return (a - b + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * ((a >= md) ? a % md : a) * ((b >= md) ? b % md : b)) % md; }

inline int manhatten(pii p1, pii p2) { return abs(p1.ff - p2.ff) + abs(p1.ss - p2.ss); }

inline bool kthbit(ll n, int k) { return (n >> k) & 1; }
inline ll setkthbit(ll n, int k) { return n | (1ll << k); }
inline ll unsetkthbit(ll n, int k) { return n & ~(1ll << k); }
inline ll flipkthbit(ll n, int k) { return n ^ (1ll << k); }

//LET'S PLAY THE GAME!!

/*

CHECK FOR N=1, N=2, etc;
REMOVE DEBUG STATEMENTS
CHECK FOR DUPLICATES (SET -> MULTISET)
CHECK FOR OVERFLOW
CLEAR EVERYTHING BEFORE EVERY TESTCASE
DON'T MISTAKE ROOT FOR LEAF
DON'T ALWAYS IGNORE THE BRUTE FORCE SOLUTION!

*/

//#define int long long //UNCOMMENT IF NEEDED

const int mx = 100008;

class segTreeMin
{
public:
    int seg[4 * mx];

    segTreeMin()
    {
        mem(seg, 0);
    }

    void build(int *a, int l, int r, int idx)
    {
        if (l > r)
            return;
        if (l == r)
        {
            seg[idx] = a[l];
            return;
        }
        int mid = l + (r - l) / 2;
        build(a, l, mid, idx << 1);
        build(a, mid + 1, r, (idx << 1) ^ 1);
        seg[idx] = min(seg[idx << 1], seg[(idx << 1) ^ 1]);
    }

    int getMin(int idx, int l, int r, int qs, int qe)
    {
        if (l > r || l > qe || r < qs)
            return INT_MAX;
        if (l >= qs && r <= qe)
            return seg[idx];
        int mid = l + (r - l) / 2;
        return min(getMin(idx << 1, l, mid, qs, qe), getMin((idx << 1) ^ 1, mid + 1, r, qs, qe));
    }
};

class segTreeMax
{
public:
    int seg[4 * mx];

    segTreeMax()
    {
        mem(seg, 0);
    }

    void build(int *a, int l, int r, int idx)
    {
        if (l > r)
            return;
        if (l == r)
        {
            seg[idx] = a[l];
            return;
        }
        int mid = l + (r - l) / 2;
        build(a, l, mid, idx << 1);
        build(a, mid + 1, r, (idx << 1) ^ 1);
        seg[idx] = max(seg[idx << 1], seg[(idx << 1) ^ 1]);
    }

    int getMax(int idx, int l, int r, int qs, int qe)
    {
        if (l > r || l > qe || r < qs)
            return 0;
        if (l >= qs && r <= qe)
            return seg[idx];
        int mid = l + (r - l) / 2;
        return max(getMax(idx << 1, l, mid, qs, qe), getMax((idx << 1) ^ 1, mid + 1, r, qs, qe));
    }
};

int32_t main()
{
    fast;

    int k, m, n;
    int x, q, l, r, y;

    string s, t, u;

    auto start = high_resolution_clock::now();

    int tcnt = 0;

    int tests = 1;
    //pre();
    // cin >> tests;

    while (tests--)
    {
        //cout << "Case #" << ++tcnt << ": ";
        cin >> n;
        int a[n + 1];
        f(i, 1, 1 + n) cin >> a[i];
        int ng[n + 1], pg[n + 1];
        mem(pg, 0);
        f(i, 1, 1 + n) ng[i] = n + 1;
        stack<int> st;
        f(i, 1, 1 + n)
        {
            while (!st.empty() && a[st.top()] < a[i])
            {
                ng[st.top()] = i;
                st.pop();
            }
            st.push(i);
        }
        reverse(a + 1, a + 1 + n);
        while (!st.empty())
            st.pop();
        f(i, 1, 1 + n)
        {
            while (!st.empty() && a[st.top()] < a[i])
            {
                pg[n + 1 - st.top()] = n + 1 - i;
                st.pop();
            }
            st.push(i);
        }
        reverse(a + 1, a + 1 + n); //dbgc(pg);dbgc(ng);
        int pref[n + 1];
        pref[0] = 0;
        f(i, 1, 1 + n) pref[i] = pref[i - 1] + a[i];
        segTreeMin segt;
        segTreeMax segt2;
        segt.build(pref, 0, n, 1);
        segt2.build(pref, 0, n, 1);
        int ans = 0;
        f(i, 1, 1 + n)
        {
            l = pg[i], r = ng[i]; //cout<<l<<","<<r<<",";
            int lft = segt.getMin(1, 0, n, l, i - 1), rgt = segt2.getMax(1, 0, n, i + 1, r - 1);

            // DOES IT HURT TO MAKE STRONG PRETESTS ??????
            // THIS FST SUCKED THE SOUL OUT OF ME AND MY RATING!! IT COST ME THE ORANGE COLOR..
            // LOOKS LIKE I WILL BE ORANGE OVER MIKE'S DEAD BODY NOW!!
            // SEDLYF !!

            if (!rgt || rgt < pref[i])
                rgt = pref[i];
            //cout<<lft<<sp<<rgt<<sp<<a[i]<<nl;
            cmax(ans, rgt - lft - a[i]);
        }
        cout << ans;
    }
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cerr << "Time taken: "
         << duration.count() / 1000000.0 << "seconds" << nl;
    return 0;
}

// AND THE GAME IS OVER !!