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.
/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class YetAnotherTask {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        int[] a = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            a[i] = sc.nextInt();
        }
        int[] prefix = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            prefix[i] = prefix[i - 1] + a[i];
        }
        RMQMax rmq = new RMQMax(a);
        RMQMax preMax= new RMQMax(prefix);
        RMQMin preMin = new RMQMin(prefix);
        int ans = 0;
        for (int i = 1; i <= N; i++) {
            int lo = 1;
            int hi = i;
            while (lo < hi) {
                // last pos where max is me, 0011
                int mid = (lo + hi) / 2;
                if (rmq.query(mid, i) <= a[i]) {
                    hi = mid;
                }
                else {
                    lo = mid + 1;
                }
            }
            int l = lo;
            lo = i;
            hi = N;
            // last pos where max is me 1100
            while (lo < hi) {
                int mid = (lo + hi + 1) / 2;
                if (rmq.query(i, mid) <= a[i]) {
                    lo = mid;
                }
                else {
                    hi = mid - 1;
                }
            }
            int r = lo;
            if (l < r) {
                // let's force i to be included
                int total = preMax.query(i, r);
                // so, we'd have to subtract out sonething before i.
                // we can subtract i - 1, i - 2, ... l, l - 1.
                // these leave behind nothing, 1, 2, ... all but one, and all.
                int sub = preMin.query(l - 1, i - 1);
                int amt = total - sub - a[i];
                ans = Math.max(ans, amt);
            }
        }
        out.println(ans);
        out.close();
    }

    static class RMQMax {
        int[][] table;
        public RMQMax(int[] a) {
            int k = -1;
            while ((1 << (k + 1)) <= a.length) {
                k++;
            }
            table = new int[k + 1][a.length];
            for (int i = 0; i < a.length; i++) {
                table[0][i] = a[i];
            }

            for (int p = 1; p <= k; p++) {
                int len = 1 << (p - 1);
                for (int i = 0; i < a.length; i++) {
                    table[p][i] = Math.max(table[p - 1][i], table[p - 1][Math.min(a.length - 1, i + len)]);
                }
            }
        }

        public int query(int i, int j) {
            int len = j - i + 1;
            int k = -1;
            while ((1 << (k + 1)) <= len) {
                k++;
            }
            return Math.max(table[k][i], table[k][j - (1 << k) + 1]);
        }
    }

    static class RMQMin {
        int[][] table;
        public RMQMin(int[] a) {
            int k = -1;
            while ((1 << (k + 1)) <= a.length) {
                k++;
            }
            table = new int[k + 1][a.length];
            for (int i = 0; i < a.length; i++) {
                table[0][i] = a[i];
            }

            for (int p = 1; p <= k; p++) {
                int len = 1 << (p - 1);
                for (int i = 0; i < a.length; i++) {
                    table[p][i] = Math.min(table[p - 1][i], table[p - 1][Math.min(a.length - 1, i + len)]);
                }
            }
        }

        public int query(int i, int j) {
            int len = j - i + 1;
            int k = -1;
            while ((1 << (k + 1)) <= len) {
                k++;
            }
            return Math.min(table[k][i], table[k][j - (1 << k) + 1]);
        }
    }

    static class FastScanner {
        private int BS = 1<<16;
        private char NC = (char)0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public double[] nextDoubles(int N) {
            double[] res = new double[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextDouble();
            }
            return res;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }

    static void ASSERT(boolean assertion, String message) {
        if (!assertion) throw new AssertionError(message);
    }
}