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.*;
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;
        }
    }

}