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.

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws Exception {
        ScanReader in = new ScanReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        Solver main = new Solver();
        int test = 1;
        for (int i = 1; i <= test; i++) main.solve(i, in, out);
        out.close();
    }
}

class Solver {

    public void solve(int testNumber, ScanReader in, PrintWriter out) throws IOException {
        int n = in.scanInt();
        int[] ar = new int[n];
        for (int i = 0; i < n; i++) ar[i] = in.scanInt();
        int[] nextGreater = new int[n];
        Arrays.fill(nextGreater, n);
        int[] previousGreater = new int[n];
        Arrays.fill(previousGreater, -1);
        {
            Stack<Integer> st = new Stack<>();
            for (int i = 0; i < n; i++) {
                while (st.size() > 0 && ar[st.peek()] < ar[i]) nextGreater[st.pop()] = i;
                st.push(i);
            }
        }
        {
            Stack<Integer> st = new Stack<>();
            for (int i = n - 1; i >= 0; i--) {
                while (st.size() > 0 && ar[st.peek()] < ar[i]) previousGreater[st.pop()] = i;
                st.push(i);
            }
        }
        int[] sum = new int[n];
        sum[0] = ar[0];
        for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + ar[i];
        int[][] min = new int[n][17];
        int[][] max = new int[n][17];
        for (int i = 0; i < n; i++) min[i][0] = sum[i];
        for (int i = 0; i < n; i++) max[i][0] = sum[i];
        for (int j = 1; j < 17; j++) {
            for (int i = 0; i + (1 << j) <= n; i++) {
                min[i][j] = Math.min(min[i][j - 1], min[i + (1 << (j - 1))][j - 1]);
                max[i][j] = Math.max(max[i][j - 1], max[i + (1 << (j - 1))][j - 1]);
            }
        }
        int ans = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int leftSum = 0;
            if (previousGreater[i] + 1 <= i - 1) {
                int l = previousGreater[i] + 1;
                int r = i - 1;
                int lg = (int) (Math.log(r - l + 1) / Math.log(2));
                leftSum = Math.max(sum[i - 1] - (l - 1 >= 0 ? sum[l - 1] : 0), sum[i - 1] - Math.min(min[l][lg], min[r - (1 << lg) + 1][lg]));
            }
            int rightSum = 0;
            if (nextGreater[i] - 1 >= i + 1) {
                int l = i;
                int r = nextGreater[i] - 1;
                int lg = (int) (Math.log(r - l + 1) / Math.log(2));
                rightSum = Math.max(max[l][lg], max[r - (1 << lg) + 1][lg]) - sum[i];
            }
            ans = Math.max(leftSum + rightSum, ans);
        }
        out.println(ans);
    }
}

class ScanReader {
    private final byte[] buf = new byte[4 * 1024];
    private final BufferedInputStream in;
    private int index;
    private int total;

    public ScanReader(InputStream inputStream) {
        in = new BufferedInputStream(inputStream);
    }

    private int scan() throws IOException {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0) return -1;
        }
        return buf[index++];
    }

    public char scanChar() throws IOException {
        int c = scan();
        while (isWhiteSpace(c)) c = scan();
        return (char) c;
    }


    public int scanInt() throws IOException {
        int integer = 0;
        int n = scan();
        while (isWhiteSpace(n)) n = scan();
        int neg = 1;
        if (n == '-') {
            neg = -1;
            n = scan();
        }
        while (!isWhiteSpace(n)) {
            if (n >= '0' && n <= '9') {
                integer *= 10;
                integer += n - '0';
                n = scan();
            }
        }
        return neg * integer;
    }

    public String scanString() throws IOException {
        int c = scan();
        while (isWhiteSpace(c)) c = scan();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = scan();
        } while (!isWhiteSpace(c));
        return res.toString();
    }

    private boolean isWhiteSpace(int n) {
        return n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1;
    }

    public long scanLong() throws IOException {
        long integer = 0;
        int n = scan();
        while (isWhiteSpace(n)) n = scan();
        int neg = 1;
        if (n == '-') {
            neg = -1;
            n = scan();
        }
        while (!isWhiteSpace(n)) {
            if (n >= '0' && n <= '9') {
                integer *= 10;
                integer += n - '0';
                n = scan();
            }
        }
        return neg * integer;
    }

    public void scanLong(long[] A) throws IOException {
        for (int i = 0; i < A.length; i++) A[i] = scanLong();
    }

    public void scanInt(int[] A) throws IOException {
        for (int i = 0; i < A.length; i++) A[i] = scanInt();
    }

    public double scanDouble() throws IOException {
        int c = scan();
        while (isWhiteSpace(c)) c = scan();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = scan();
        }
        double res = 0;
        while (!isWhiteSpace(c) && c != '.') {
            if (c == 'e' || c == 'E') {
                return res * Math.pow(10, scanInt());
            }
            res *= 10;
            res += c - '0';
            c = scan();
        }
        if (c == '.') {
            c = scan();
            double m = 1;
            while (!isWhiteSpace(c)) {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, scanInt());
                }
                m /= 10;
                res += (c - '0') * m;
                c = scan();
            }
        }
        return res * sgn;
    }

}