    //For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;
    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int n,i,j;
            cin>>n;
            ll ar[n+1],sum=0,maxi=0,ct=0,ans=0;
            for(i=1;i<=n;++i)
            {
                cin>>ar[i];
                ans=max(ans,ct+maxi);
                sum=sum+ar[i];
                maxi=max(maxi,sum);
                ct=ct+sum;
                ans=max(ans,ct);
            }
            cout<<ans<<endl;
        }
        return 0;
    }

        #pragma GCC optimize("Ofast")
        #include<bits/stdc++.h>
        #define vll vector<long long>
        #define vi vector<int>
        #define ll long long 
        #define mp make_pair    
        #define p push
        #define pb push_back
        #define np next_permutation
        #define mpl map<ll,ll> 
        #define fin(i,a,b) for(int i=a;i<=b;i++)
        #define finr(i,a,b) for(int i=a;i>=b;--i)
        #define max3(a,b,c) max(max(a,b),c)
        #define min3(a,b,c) min(min(a,b),c)
        #define all(v) v.begin(),v.end()
        #define allr(v) v.rbegin(),v.rend()
        #define debug(x) cerr << #x << " = " << x << endl;
        const ll INF =1e18;
        const ll mod =1e9+7;
        using namespace std;
        int main(){
               int n;
               cin>>n;
              // vll v;
               ll pref[n+1];
               pref[0]=0;
               ll end[n+1];
               end[0]=0;
               fin(i,1,n){
                   ll x;
                   cin>>x;
                   pref[i]=pref[i-1]+x;
                   end[i]=end[i-1]+pref[i];
                   
                   
                  
               }
               ll ans=0;
               ll mx=0;
               fin(i,1,n){
                  mx=max(mx,pref[i]);
                  ans=max(ans,end[i-1]+mx);
                  
               }
               cout<<ans<<endl;
              

            return 0;
        }
        

        

            
            


                
            
        
                
            
            
            
            
