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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Vector;
import java.io.IOException;
import java.util.Stack;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Indrajit Sinha
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		DYetAnotherYetAnotherTask solver = new DYetAnotherYetAnotherTask();
		solver.solve(1, in, out);
		out.close();
	}

	static class DYetAnotherYetAnotherTask {
		PrintWriter out;
		InputReader in;
		int testcase;
		int n;
		Node[] ptree;
		Node[] stree;

		void build(int a[], int index, int beg, int end, Node[] tree) {
			if (beg == end) {

				// If there is one element in array,
				// store it in current node of
				// segment tree
				tree[index].sum = a[beg];
				tree[index].prefix = Math.max(0, a[beg]);
			} else {
				int mid = (beg + end) / 2;

				// If there are more than one elements,
				// then recur for left and right subtrees
				build(a, 2 * index + 1, beg, mid, tree);
				build(a, 2 * index + 2, mid + 1, end, tree);

				// adds the sum and stores in the index
				// position of segment tree
				tree[index].sum = tree[2 * index + 1].sum +
						tree[2 * index + 2].sum;

				// stores the max of prefix-sum either
				// from right or from left.
				tree[index].prefix = Math.max(tree[2 * index + 1].prefix,
						tree[2 * index + 1].sum +
								tree[2 * index + 2].prefix);
			}
		}

		Node query(int index, int beg, int end, int l, int r, Node[] tree) {
			Node result = new Node();
			result.sum = result.prefix = 0;

			// If segment of this node is outside the given
			// range, then return the minimum value.
			if (beg > r || end < l)
				return result;

			// If segment of this node is a part of given
			// range, then return the node of the segment
			if (beg >= l && end <= r)
				return tree[index];

			int mid = (beg + end) / 2;

			// if left segment of this node falls out of
			// range, then recur in the right side of
			// the tree
			if (l > mid)
				return query(2 * index + 2, mid + 1, end,
						l, r, tree);

			// if right segment of this node falls out of
			// range, then recur in the left side of
			// the tree
			if (r <= mid)
				return query(2 * index + 1, beg, mid,
						l, r, tree);

			// If a part of this segment overlaps with
			// the given range
			Node left = query(2 * index + 1, beg, mid,
					l, r, tree);
			Node right = query(2 * index + 2, mid + 1,
					end, l, r, tree);

			// adds the sum of the left and right
			// segment
			result.sum = left.sum + right.sum;

			// stores the max of prefix-sum
			result.prefix = Math.max(left.prefix, left.sum +
					right.prefix);

			// returns the value
			return result;
		}

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int t, i, j, tt, k;
			this.testcase = testNumber;
			this.out = out;
			this.in = in;
			n = in.nextInt();
			ptree = new Node[4 * n];
			stree = new Node[4 * n];
			for (i = 0; i < ptree.length; i++) {
				ptree[i] = new Node();
				stree[i] = new Node();
			}
			int a[] = new int[n];
			int b[] = new int[n];
			for (i = 0; i < n; i++) {
				a[i] = in.nextInt();
				b[n - i - 1] = a[i];
			}
			build(a, 0, 0, n - 1, ptree);
			build(b, 0, 0, n - 1, stree);
			Stack<Integer> nn = new Stack();
			Stack<Integer> p = new Stack();
			int next[] = new int[n];
			int prev[] = new int[n];
			nn.push(0);
			p.push(0);
			for (i = 1; i < n; i++) {
				if (nn.isEmpty())
					nn.push(i);
				if (p.isEmpty())
					p.push(i);
				while (nn.isEmpty() == false && a[nn.peek()] < a[i]) {
					next[nn.pop()] = i;

				}

				nn.push(i);
				while (p.isEmpty() == false && b[p.peek()] < b[i]) {
					prev[n - p.pop() - 1] = i;
				}

				p.push(i);
			}
			while (nn.isEmpty() == false) {
				next[nn.pop()] = n;
			}
			while (p.isEmpty() == false) {
				prev[n - p.pop() - 1] = n;
			}
			int mx = 0;
			for (i = 0; i < n; i++) {
				int rangel = prev[i] - 1;
				int ranger = next[i] - 1;
				int ss = query(0, 0, n - 1, n - i, rangel, stree).prefix;
				ss += query(0, 0, n - 1, i + 1, ranger, ptree).prefix;
				mx = Math.max(mx, ss);
			}
			pn(mx);
		}

		void pn(int zx) {
			out.println(zx);
		}

		class Node {
			int sum;
			int prefix;

		}

	}

	static class InputReader {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (numChars == -1) {
				throw new UnknownError();
			}
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new UnknownError();
				}
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public String next() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuffer res = new StringBuffer();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));

			return res.toString();
		}

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

	}
}

