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 Main implements Runnable{
    FastScanner sc;
    PrintWriter pw;
    final class FastScanner {
        BufferedReader br;
        StringTokenizer st;
 
        public FastScanner() {
            try {
                br = new BufferedReader(new InputStreamReader(System.in));
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        public long nlo() {
            return Long.parseLong(next());
        }
 
        public String next() {
            if (st.hasMoreTokens()) return st.nextToken();
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }
 
        public int ni() {
            return Integer.parseInt(next());
        }
 
        public String nli() {
            String line = "";
            if (st.hasMoreTokens()) line = st.nextToken();
            else try {
                return br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (st.hasMoreTokens()) line += " " + st.nextToken();
            return line;
        }
 
        public double nd() {
            return Double.parseDouble(next());
        }
    }
    public static void main(String[] args) throws Exception
    {
        new Thread(null,new Main(),"codeforces",1<<28).start();
    }
    public void run()
    {
        sc=new FastScanner();
        pw=new PrintWriter(System.out);
        solve();
        pw.flush();
        pw.close();
    }
    public long gcd(long a,long b)
    {
        return b==0L?a:gcd(b,a%b);
    }
    public long ppow(long a,long b,long mod)
    {
        if(b==0L)
        return 1L;
        long tmp=1;
        while(b>1L)
        {
            if((b&1L)==1)
            tmp*=a;
            a*=a;
            a%=mod;
            tmp%=mod;
            b>>=1;
        }
        return (tmp*a)%mod;
    }
    public  int gcd(int x,int y)
    {
        return y==0?x:gcd(y,x%y);
    }
    double one=1;
    double two=2;
    //////////////////////////////////
    /////////////  LOGIC  ///////////
    ////////////////////////////////
    public void solve()
    {
        int T=1;
        while(T-->0)
        {
           int n=sc.ni();
           int[] arr=new int[n];
           int tmp=-50;
           for(int i=0;i<n;i++)
           {arr[i]=sc.ni();tmp=Math.max(tmp,arr[i]);}
           int[] prr=new int[n];
           prr[0]=arr[0];
           for(int i=1;i<n;i++)
           prr[i]=prr[i-1]+arr[i];
           int[] lrr=new int[n];
           int[] rrr=new int[n];
           Stack<Integer> stk=new Stack<Integer>();
           Arrays.fill(lrr,-1);
           Arrays.fill(rrr,n);
           for(int i=0;i<n;i++)
           {
               while(stk.size()>0&&arr[stk.peek()]<=arr[i])
               stk.pop();
               if(stk.size()>0)
               lrr[i]=stk.peek();
               stk.push(i);
           }
           stk.clear();
           for(int i=n-1;i>=0;i--)
           {
               while(stk.size()>0&&arr[stk.peek()]<=arr[i])
               stk.pop();
               if(stk.size()>0)
               rrr[i]=stk.peek();
               stk.push(i);
           }
           int[] segmn=new int[4*n+1];
           int[] segmx=new int[4*n+1];
           buildmn(prr,0,n-1,segmn,1);
           buildmx(prr,0,n-1,segmx,1);
           int ans=prr[n-1]-tmp;
           for(int i=0;i<n;i++)
           {
               int a=0,b=0;
               if(i>0)
               {
                   int l=Math.max(0,lrr[i]);
                   int r=i-1;
                   if(l<=r)
                       a=Math.max(0,prr[i-1]-qmn(segmn,1,0,n-1,l,r));
                       if(lrr[i]==-1)
                       a=Math.max(a,prr[i-1]);
               }
               if(i!=n-1)
               {
                   int l=i+1;
                   int r=rrr[i]-1;
                   if(l<=r)
                   b=Math.max(0,qmx(segmx,1,0,n-1,l,r)-prr[i]);
               }
               ans=Math.max(ans,a+b);
               //pw.println(lrr[i]+" "+rrr[i]+" "+a+" "+b);
           }
           pw.println(ans);
        }
    }
    public void buildmn(int[] arr,int s,int e,int[] seg,int ind)
    {
        if(e<s)
        return ;
        if(s==e)
        seg[ind]=arr[s];
        else
        {
            buildmn(arr,s,(s+e)/2,seg,2*ind);
            buildmn(arr,(s+e)/2+1,e,seg,2*ind+1);
            seg[ind]=Math.min(seg[2*ind],seg[2*ind+1]);
        }
    }
    public void buildmx(int[] arr,int s,int e,int[] seg,int ind)
    {
        if(e<s)
        return ;
        if(s==e)
        seg[ind]=arr[s];
        else
        {
            buildmx(arr,s,(s+e)/2,seg,2*ind);
            buildmx(arr,(s+e)/2+1,e,seg,2*ind+1);
            seg[ind]=Math.max(seg[2*ind],seg[2*ind+1]);
        }
    }
    public int qmn(int[] seg,int ind,int s,int e,int qs,int qe)
    {
        if(qs<=s&&qe>=e)
        return seg[ind];
        if(qe<s||qs>e)
        return Integer.MAX_VALUE;
        return Math.min(qmn(seg,2*ind,s,(s+e)/2,qs,qe),qmn(seg,2*ind+1,(s+e)/2+1,e,qs,qe));
    }
    public int qmx(int[] seg,int ind,int s,int e,int qs,int qe)
    {
        if(qs<=s&&qe>=e)
        return seg[ind];
        if(qe<s||qs>e)
        return Integer.MIN_VALUE;
        return Math.max(qmx(seg,2*ind,s,(s+e)/2,qs,qe),qmx(seg,2*ind+1,(s+e)/2+1,e,qs,qe));
    }
}




