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

import java.io.*;
import java.math.BigInteger;
import java.util.*;

/*
procrastinating
*/

public class Main {

    static FastReader in;
    static PrintWriter out;
    static Random rand = new Random();
    static final int oo = (int) 2e9;
    static final long OO = (long) 4e18;
    static final int MOD = 998244353;

    static void dbg(Object... args) {
        for (Object a : args)
            out.print(a + " ");
        out.println();
    }

    static class SegTree {
        class Data {
            int sum, max, maxPre, maxSuf;

            Data() {
            }
        }
        int sz;
        Data[] t;

        Data combine(Data l, Data r) {
            Data res = new Data();
            res.sum = l.sum + r.sum;
            res.maxPre = Math.max(l.maxPre, l.sum + r.maxPre);
            res.maxSuf = Math.max(r.maxSuf, l.maxSuf + r.sum);
            res.max = Math.max(Math.max(l.max, r.max), l.maxSuf + r.maxPre);
            return res;
        }

        Data newData(int x) {
            Data res = new Data();
            res.sum = x;
            res.maxPre = res.maxSuf = res.max = Math.max(0, x);
            return res;
        }

        void build(int v, int tl, int tr, int[] arr) {
            if (tl == tr) {
                t[v] = newData(arr[tl]);
                return;
            }
            int tm = (tl + tr) / 2;
            build(v * 2 + 1, tl, tm, arr);
            build(v * 2 + 2, tm + 1, tr, arr);
            t[v] = combine(t[v * 2 + 1], t[v * 2 + 2]);
        }
        void build(int[] arr) {
            this.sz = arr.length;
            t = new Data[sz * 4];
            build(0, 0, sz - 1, arr);
        }

        void upd(int v, int tl, int tr, int ind, int val) {
            if (tl == tr) {
                t[v] = newData(val);
                return;
            }
            int tm = (tl + tr) / 2;
            if (ind <= tm) {
                upd(v * 2 + 1, tl, tm, ind, val);
            } else {
                upd(v * 2 + 2, tm + 1, tr, ind, val);
            }
            t[v] = combine(t[v * 2 + 1], t[v * 2 + 2]);
        }
        void upd(int ind, int val) {
            upd(0, 0, sz - 1, ind, val);
        }

        Data query(int v, int tl, int tr, int l, int r) {
            if (l <= tl && tr <= r) {
                return t[v];
            }
            int tm = (tl + tr) / 2;
            if (r <= tm) {
                return query(v * 2 + 1, tl, tm, l, r);
            } else if (l > tm) {
                return query(v * 2 + 2, tm + 1, tr, l, r);
            }
            return combine(query(v * 2 + 1, tl, tm, l, tm),
                        query(v * 2 + 2, tm + 1, tr, tm + 1, r));
        }
        Data query(int l, int r) {
            return query(0, 0, sz - 1, l, r);
        }
    }


    static void solve() {
        int n = in.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();

        SegTree st = new SegTree();
        st.build(a);

        Stack<Integer> stack = new Stack<>();
        int[] pre = new int[n];
        for (int i = 0; i < n; i++) {
            while (!stack.empty() && a[stack.peek()] <= a[i])
                stack.pop();
            if (stack.empty()) {
                pre[i] = 0;
            } else {
                pre[i] = stack.peek() + 1;
            }
            stack.push(i);
        }
        stack.clear();
        int[] suf = new int[n];
        for (int i = n - 1; i >= 0; i--) {
            while (!stack.empty() && a[stack.peek()] <= a[i])
                stack.pop();
            if (stack.empty()) {
                suf[i] = n - 1;
            } else {
                suf[i] = stack.peek() - 1;
            }
            stack.push(i);
        }

        int ans = 0;
        for (int i = 0; i < n; i++) {
            int l = pre[i];
            int r = suf[i];
            int val = a[i];
//            out.println(l + " " + r);
            st.upd(i, 0);
            SegTree.Data d1 = st.query(l, i);
            SegTree.Data d2 = st.query(i, r);
            ans = Math.max(ans, d1.maxSuf + d2.maxPre);
            st.upd(i, val);
//            out.println(maxData.sum + " " + maxData.maxPre + " " + maxData.maxSuf + " " + maxData.max);
//            out.flush();
        }

        out.println(ans);
    }

    public static void main(String[] args) throws FileNotFoundException {
        in = new FastReader(System.in);
//        in = new FastReader(new FileInputStream("sometext.txt"));
        out = new PrintWriter(System.out);
//        out = new PrintWriter(new FileOutputStream("output.txt"));


        solve();

//        Thread thread = new Thread(null, () -> {
//            solve();
//        }, "Main", 1 << 28);
//        thread.start();
//        try {
//            thread.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }


//        out.flush();
        out.close();
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        FastReader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        Integer nextInt() {
            return Integer.parseInt(next());
        }

        Long nextLong() {
            return Long.parseLong(next());
        }

        Double nextDouble() {
            return Double.parseDouble(next());
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }
    }

}