Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value a_i. 

First, Alice chooses a non-empty consecutive segment of cards [l; r] (l ≤ r). After that Bob removes a single card j from that segment (l ≤ j ≤ r). The score of the game is the total value of the remaining cards on the segment (a_l + a_{l + 1} + ... + a_{j - 1} + a_{j + 1} + ... + a_{r - 1} + a_r). In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is 0.

Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.

What segment should Alice choose so that the score is maximum possible? Output the maximum score.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of cards.

The second line contains n integers a_1, a_2, ..., a_n (-30 ≤ a_i ≤ 30) — the values on the cards.

Output

Print a single integer — the final score of the game.

Examples

Input


5
5 -2 10 -1 4


Output


6


Input


8
5 2 5 3 -30 -30 6 9


Output


10


Input


3
-10 6 -15


Output


0

Note

In the first example Alice chooses a segment [1;5] — the entire row of cards. Bob removes card 3 with the value 10 from the segment. Thus, the final score is 5 + (-2) + (-1) + 4 = 6.

In the second example Alice chooses a segment [1;4], so that Bob removes either card 1 or 3 with the value 5, making the answer 5 + 2 + 3 = 10.

In the third example Alice can choose any of the segments of length 1: [1;1], [2;2] or [3;3]. Bob removes the only card, so the score is 0. If Alice chooses some other segment then the answer will be less than 0.
/*

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());
        }
    }
}