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




