10
-3 4 -4 1 1 -4 -5 3 -4 -2
 10
8 9 -10 -10 -4 -3 5 9 -9 9
 6
5 -4 2 -1 2 -2
 11
3 0 1 -2 5 -5 -1 0 3 2 2
 10
4 -2 -2 -2 2 0 1 0 -4 -1
 4
2 2 -3 4
 11
-11 -7 -2 -24 30 -26 24 9 -8 -9 23
 6
30 -29 5 5 -29 30
 24
-8 -6 6 -11 -15 -7 10 -19 -6 21 14 -4 -21 -10 12 17 1 -1 5 19 2 0 -4 -23
 10
-9 -6 -1 9 -2 0 0 -1 5 0
 6
10 -8 3 3 -8 10
 10
-2 -3 -1 4 -3 2 0 1 -5 4
 5
30 -29 10 -1 10
 31
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -29 30
 7
30 -20 5 1 3 -20 30
 5
30 -29 4 -3 4
 10
-5 28 -18 -10 9 -2 11 -6 -19 30
 8
4 -10 13 -13 2 2 -11 6
 131
30 -29 29 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 27 -26 27
 10
-4 4 0 -2 3 0 1 2 3 -4
 10
-2 -8 5 4 -1 -6 -9 6 1 -6
 7
25 -17 13 13 -25 25 -24
 10
3 -3 2 1 -4 -2 3 -2 -3 0
 10
4 -5 0 4 0 0 -3 1 1 -4
 10
0 4 4 1 -3 4 2 1 4 -1
 17
15 -13 7 8 8 1 -19 12 8 -23 -4 16 -13 -28 10 -30 -14
 4
1 1 -4 10
 11
3 -2 1 1 1 1 1 1 1 -3 4
 7
30 -29 10 10 10 -29 30
 7
25 -13 17 -23 12 12 9
 7
15 -14 10 10 10 -14 15
 10
3 0 0 -4 3 1 -2 -1 -1 4
 6
8 -7 7 -6 5 5
 10
1 -4 -4 4 -2 -3 3 0 -2 3
 15
1 -26 -6 26 7 10 -2 -4 -6 14 -9 25 9 -12 -28
 6
30 -29 10 10 -29 30
 1
6
 2
1 1
 5
0 5 -4 3 -1
 7
5 -4 3 -1 3 -4 4
 10
16 -15 13 -1 10 -2 -20 26 5 -30
 10
-1 2 0 -1 2 -1 4 3 -5 1
 7
5 -4 1 1 1 -4 5
 5
10 -3 -5 3 5
 10
4 -4 3 -2 3 -4 -2 0 1 0
 10
-5 -5 -3 1 -2 2 0 2 -3 4
 10
2 -4 -2 3 1 1 -2 -1 -3 4
 10
-4 -5 4 -3 2 -2 2 1 -1 0
 10
0 -1 4 -2 3 -2 -1 -5 -5 2
 5
19 -6 1 -11 19
 10
1 0 -4 3 -5 3 -1 2 2 -5
 7
5 -5 3 4 3 -5 5
 10
-5 2 -3 -3 -4 3 3 0 2 4
 4
2 3 -10 11
 10
27 -17 15 -22 18 17 -6 2 16 -14
 6
30 -22 6 7 -22 30
 7
30 -25 0 6 9 -2 10
 9
7 2 -1 1 1 -10 6 -1 6
 4
1 -10 1 10
 5
17 -16 12 -11 15
 35
-23 -22 23 4 -22 29 -4 -6 -28 18 -5 21 -7 -8 -11 -7 30 -25 -1 12 19 5 -8 12 -1 10 -24 -19 24 -17 -7 24 20 -22 16
 10
4 -3 1 -2 1 1 0 -4 4 -2
 10
4 -2 1 0 1 -5 -2 1 0 -5
 2
3 9
 9
11 -5 -5 1 2 3 -5 -5 11

1
 8
 1
 4
 1
 2
 15
 5
 36
 2
 3
 1
 9
 28
 4
 1
 7
 2
 1
 7
 4
 13
 1
 1
 13
 16
 1
 6
 20
 21
 20
 1
 5
 1
 44
 10
 0
 1
 0
 2
 9
 5
 2
 3
 1
 2
 2
 1
 1
 3
 3
 6
 8
 2
 29
 6
 13
 5
 1
 1
 30
 1
 1
 3
 3

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

	}
}

