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.util.*;
import java.io.*;
public class D{
	
	static class LineTree{
		static class Node{
			int l;
			int r;
			int max;
			Node(int l, int r, int max){
				this.l = l;
				this.r = r;
				this.max = max;
			}
			
			public String toString(){
				return new Formatter().format("(%d,%d,%d)",l,r,max).toString();
			}
		}
		
		
		Node[] arr;
		int size;
		// int n;
		LineTree(int n, int[] a){
			int powOf2Count = 0;
			int powOf2=1;
			while(powOf2 < n){
				powOf2*=2;
				powOf2Count++;
			}
			// this.n = n;
			size = n;
			arr = new Node[powOf2*2-1];
			arr[0] = new Node(0,powOf2-1,Integer.MIN_VALUE);
			constructTree(0,a);
		}
		
		void constructTree(int i,int[] a){
			if(arr[i].l == arr[i].r){
				if(arr[i].l >= size) arr[i].max = Integer.MIN_VALUE;
				else arr[i].max = a[arr[i].l];
				return;
			}
			int l = arr[i].l;
			int r = arr[i].r;
			
			arr[i*2 +1 ] = new Node(l,(r+l)/2,Integer.MIN_VALUE);
			arr[i*2 +2 ] = new Node((r+l)/2+1,r,Integer.MIN_VALUE);
			constructTree(i*2+1,a);
			constructTree(i*2+2,a);
			arr[i].max = Math.max(arr[i*2 +1 ].max,arr[i*2 +2 ].max);
		}
		
		int max(int l, int r, int i){
			// System.out.println(new Formatter().format("%d:(%d,%d)", i, l, r).toString());
			
			if(arr[i].l ==l && arr[i].r ==r){
				return arr[i].max;
			}
			int max = Integer.MIN_VALUE;
			if(arr[2*i+1].r >= l)
				max = Math.max(max, max(l,Math.min(arr[2*i+1].r,r),2*i+1));
			if(arr[2*i+2].l <= r)
				max = Math.max(max, max(Math.max(arr[2*i+2].l,l),r,2*i+2));
			return max;
		}
		
		public String toString(){
			return Arrays.toString(arr);
		}
		
	}
	static class Pair{
		int x;
		int y;
		Pair(int n){
			x = 0;
			y = n-1;
		}
		public String toString(){
			return new Formatter().format("(%d,%d)",x,y).toString();
		}
	}
	
	
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		
		
		int[] a = new int[n];
		int[] pref = new int[n];
		Pair[] lines = new Pair[n];
		int[] indeses = new int[61];
		for(int i = 0;i<n;++i){
			a[i] = in.nextInt();
			lines[i] = new Pair(n);
			for(int j = a[i] + 30 + 1;j<61;++j){
				lines[i].x = Math.max(lines[i].x, indeses[j]);
			}
			indeses[a[i]+30] = i+1;
			if(i == 0) pref[0] = a[0];
			else pref[i] = pref[i-1] + a[i];
		}
		for(int i = 0;i<61;++i){
			indeses[i] = n;
		}
		int[] suf = new int[n];
		for(int i = n-1;i>=0;--i){
			for(int j = a[i] + 30+ 1;j<61;++j){
				lines[i].y = Math.min(lines[i].y, indeses[j]);
			}
			indeses[a[i]+30] = i-1;
			if(i == n-1) suf[i] = a[i];
			else suf[i] = suf[i+1] + a[i];
			
		}
		
		// System.out.println(Arrays.toString(lines));
		LineTree preft = new LineTree(n,pref);
		// System.out.println(Arrays.toString(pref));
		// // System.out.println(preft);
		// // System.out.println(preft.max(0,3,0));
		// // System.out.println(preft.max(1,3,0));
		// // System.out.println(preft.max(3,3,0));
		// // System.out.println(preft.max(3,4,0));
		// // System.out.println(preft.max(4,4,0));
		// // System.out.println(preft.max(4,4,0));
		LineTree suft = new LineTree(n,suf);
		// System.out.println(Arrays.toString(suf));
		// System.out.println(suft);
		int res = 0;
		for(int i = 0;i<n;++i){
			Pair p = lines[i];
			int maxSum = suft.max(p.x,i,0) + preft.max(i,p.y,0);
			// System.out.println(i + " " + maxSum);
			maxSum  =maxSum -  suf[i] - pref[i];
			
			// System.out.println(new Formatter().format("%d:%d + %d = %d", i, suft.max(p.x,i,0),preft.max(i,p.y,0),maxSum).toString());
			res = Math.max(res, maxSum);
		}
		System.out.println(res);
		
	}
}