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

/*

I FOLLOW THE WAY OF THE RIGHT HONOURABLE JIM KWIK,
GOD BLESS YOU SIR

1.  I AM A 'NEWBIE' IN RATING
2.  I WILL FAIL MANY TIMES, BUT I WILL NOT FAIL TO LEARN FROM FAILURE
3.  I WILL NEVER SURRENDER MY GOAL, NO MATTER HOW MUCH I FAIL
4.  I WILL PROVE MY SKILLS WHEN THE TIME COMES
5.  I WILL ALWAYS KEEP TRYING

*/





import java.util.*;
import java.io.*;
public class A {










    static class Range{
        int left, right;
        Range(){
            this.left = -1;
            this.right = -1;
        }
    }
    static class SegmentTree{
        private Pair[] tree;
        private int n, input[];
        public SegmentTree(int[] input){
            this.n = input.length-1;
            int[] temp = new int[n+1];
            for (int i = 1; i <= n; ++i){
                temp[i] = input[i];
            }
            this.input = temp;
            tree = new Pair[4*n+5];
            build(1, 1, n);
        }
        int l(int u){
            return 2*u;
        }
        int r(int u){
            return l(u)+1;
        }
        void build(int u, int left, int right){
            tree[u] = new Pair(left, right);
            if (left == right){
                tree[u].min = input[left];
                tree[u].max = input[left];
                tree[u].sum = input[left];
                return;
            }
            int mid = (left+right)/2;
            build(l(u), left, mid);
            build(r(u), mid+1, right);
            tree[u].min = Math.min(tree[l(u)].min, tree[r(u)].min);
            tree[u].max = Math.max(tree[l(u)].max, tree[r(u)].max);
            tree[u].sum = tree[l(u)].sum+tree[r(u)].sum;
        }
        void prop(int u){
            if (tree[u].left != tree[u].right){
                tree[l(u)].delta += tree[u].delta;
                tree[r(u)].delta += tree[u].delta;
                tree[u].min += tree[u].delta;
                tree[u].max += tree[u].delta;
                tree[u].delta = 0;
            }
        }
        void update(int u){
            if (tree[u].left != tree[u].right){
                tree[u].min = tree[l(u)].min+tree[l(u)].delta;
                tree[u].min = Math.min(tree[u].min,tree[r(u)].min+tree[r(u)].delta);
                tree[u].max = tree[l(u)].max+tree[l(u)].delta;
                tree[u].max = Math.max(tree[u].max,tree[r(u)].max+tree[r(u)].delta);
                tree[u].sum = tree[l(u)].sum+(tree[l(u)].right-tree[l(u)].left+1)*tree[l(u)].delta;
                tree[u].sum += tree[r(u)].sum+(tree[r(u)].right-tree[r(u)].left+1)*tree[r(u)].delta;
            }
        }
        int rangeMin(int u, int left, int right){
            if (tree[u].right < left || tree[u].left > right){
                return (int)1e9;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                return tree[u].min+tree[u].delta;
            }
            prop(u);
            int min = rangeMin(l(u), left, right);
            min = Math.min(min, rangeMin(r(u), left, right));
            update(u);
            return min;
        }
        int rangeMax(int u, int left, int right){
            if (tree[u].right < left || tree[u].left > right){
                return (int)-1e9;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                return tree[u].max+tree[u].delta;
            }
            prop(u);
            int max = rangeMax(l(u), left, right);
            max = Math.max(max, rangeMax(r(u), left, right));
            update(u);
            return max;
        }
        long rangeSum(int u, int left, int right){
            if (tree[u].right < left || tree[u].left > right){
                return 0L;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                return tree[u].sum+(tree[u].right-tree[u].left+1)*tree[u].delta;
            }
            prop(u);
            long sum = rangeSum(l(u), left, right);
            sum += rangeSum(r(u), left, right);
            update(u);
            return sum;
        }
        void rangeAdd(int u, int left, int right, int add){
            if (tree[u].right < left || tree[u].left > right){
                return;
            }
            if (tree[u].left >= left && tree[u].right <= right){
                tree[u].delta += add;
                return;
            }
            prop(u);
            rangeAdd(l(u), left, right, add);
            rangeAdd(r(u), left, right, add);
            update(u);
        }
        private class Pair{
            int min, max, left, right;
            long sum;
            int delta = 0;
            public Pair(int l, int r){
                this.left = l;
                this.right = r;
                sum = 0L;
                min = (int)1e9;
                max = -min;
            }
        }
    }





















    static boolean tests = false;
    static void solve(){
       int n = fs.nextInt();
       int[] a = new int[n+1];
       for (int i = 1; i <= n; ++i){
           a[i] = fs.nextInt();
       }
       Range[] range = new Range[n+1];
       for (int i = 0; i <= n; ++i){
           range[i] = new Range();
       }
       Stack<Integer> left = new Stack<>();
       Stack<Integer> right = new Stack<>();
       left.push(1);
       range[1].left = 1;
       for (int i = 2; i <= n; ++i){
           while (!left.isEmpty() && a[i] >= a[left.peek()]) left.pop();
           if (!left.isEmpty()) range[i].left = left.peek()+1;
           else range[i].left = 1;
           left.push(i);
       }
       range[n].right = n;
       right.push(n);
       for (int i = n-1; i >= 1; --i){
           while (!right.isEmpty() && a[i] >= a[right.peek()]) right.pop();
           if (!right.isEmpty()) range[i].right = right.peek()-1;
           else range[i].right = n;
           right.push(i);
       }
       int[] suml = new int[n+1], sumr = new int[n+1];
       for (int i = 1; i <= n; ++i){
           suml[i] = a[i]+suml[i-1];
       }
       for (int i = n; i > 0; --i){
           sumr[i] = a[i]+sumr[Math.min(i+1, n)];
       }
       SegmentTree st_left = new SegmentTree(suml);
       SegmentTree st_right = new SegmentTree(sumr);
       int ans = 0, j = -1;
       for (int i = 1; i <= n; ++i){
           // bob chooses a[i] as max
            int max_right = Math.max(0, st_left.rangeMax(1, i, range[i].right)-suml[i]);
            int max_left = Math.max(0, st_right.rangeMax(1, range[i].left, i)-sumr[i]);
            ans = Math.max(ans, max_left+max_right);
       }
       out.println(ans);
    }

























    static FastScanner fs;
    static PrintWriter out;
    static int int_max = (int)1e9, mod = int_max+7;
    public static void main(String[] args) {
        fs = new FastScanner();
        out = new PrintWriter(System.out);
        int t = tests ? fs.nextInt() : 1;
        while (t-- > 0) solve();
        out.close();
    }
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readIntArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long[] readLongArray(int n){
            long a[] = new long[n];
            for (int i = 0; i < n; ++i){
                a[i] = nextLong();
            }
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}