#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define ld long double

template <class T>
void print(vector<T>& v){
    for(T x:v) cout<<x<<" ";
    cout<<"\n";
}

void inp(vector<int>& v, int n){
    for (int i=0; i<n; ++i) scanf("%d ", &v[i]);
}

const int nax=1e+5+5;


void sol(){
    int n;
    cin>>n;
    vector<int> nums(n);
    inp(nums, n);
    // cout<<n<<endl;
    // print(nums);
    ll curr=0, res=0, a=0, b=0;
    for (ll x:nums){
        a+=x;
        b=max(a, b);
        res=max(res, curr+b);
        curr+=a;
        // cout<<"curr ="<<curr<<" z="<<z<<endl;
        // res=max(res, curr);
    }
    cout<<res<<endl;
}

int main() {
    int t;
    // cin>>t;
    t=1;
    t++;
    // std::cout << std::setprecision(15);
    for (int i=1; i<t; ++i){ 
        // printf("Case #%d: ", i);
        sol();
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define Go_ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pii pair<int ,int>
#define PII pair<ll ,ll>

const int Max=1e9+100;
const ll  MAX=1e18+500;
const int sz=2e5+100;

string str;
int dp[25][4];
int len;

int cholo(int id,int last)
{
    if(id>=len) {
       if(last==0) return 0;
       else return 100;
    }
    int &ret=dp[id][last];
    if(~ret) return ret;

    int  mx=cholo(id+1,(last + (str[id]-'0')%3)%3);

    mx=min(mx, 1+cholo(id+1, last));

    return (ret= mx);
}


int main()
{
    Go_
    ll a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,n,mx;
    char ch;
    double d;
    //tring str1,str2,str;
    
    cin>>n;

    mx=0;

    ll last=0;
    ll pre=0;
    ll le=0;

    for(ll i=1;i<=n;i++)
    {
        cin>>x;
        pre+=x;
        le=max(le, pre);
        mx=max(mx, last+le);
        last+=pre;
    }

    cout<<mx<<endl;

}
