#include <bits/stdc++.h>
#include <time.h>
using namespace std;

#define ll long long
#define ld long double
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define all(x) x.begin(),x.end()
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
#define MOD 1000000007
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
//#define endl "\n"
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

int main()
{
    IOS;
    ll t,k,n,m,d,u,v;
    t=1;
    //cin >> t;
    while(t--)
    {
        cin >> n;
        vector<ll> v(n);
        ll ans = 0, mx = 0, pref = 0, prefpref = 0;
        for(int i=0;i<n;i++)
        {
            cin >> v[i];
            pref += v[i];
            mx = max(mx,pref);
            ans = max(ans,prefpref+mx);
            prefpref += pref;
        }
        cout << ans << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
    
int main() {
    long long int N;
    cin >> N;
    vector<long long int> A(N), cumsum(N, 0);
    for(int i = 0; i < N; ++i) cin >> A.at(i);
    if(N == 1) {
        if(A.at(0) > 0) cout << A.at(0) << endl;
        else cout << 0 << endl;
        return 0;
    }

    cumsum.at(0) = A.at(0);
    for(int i = 1; i < N; ++i) {
        cumsum.at(i) = cumsum.at(i-1) + A.at(i);
    }
    // どのloop回数で最大化を探す
    long long int sum = A.at(0), candi = 0, candsum = A.at(0);
    vector<long long int> offset(N);
    offset.at(0) = A.at(0);
    // if(sum > 0) candi = 0;
    for(int i = 1; i < N; ++i) {
        candsum += cumsum.at(i);
        offset.at(i) = candsum;
        if(candsum > sum) {
            sum = candsum; // update
            candi = i;
        }
        // cout << offset.at(i) << endl;
    }
    
    long long int ans = offset.at(candi);
    // candsum = sum;
    for(int i = candi; i >= 0; --i) {
        sum -= A.at(i);
        ans = max(sum, ans);
    }
    sum = offset.at(candi);
    if(candi < N-1) {
        for(int i = 0; i <= candi+1; ++i) {
            sum += A.at(i);
            ans = max(sum, ans);
        }
    }
    cout << ans << endl;
    
}
