#include<bits/stdc++.h>
#define INFTY 10000000000
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
const int MOD=998244353;
const int di[4] = {-1,0,1,0};
const int dj[4] = {0,-1,0,1};
const int INF = 1e9;
const int A = 1000005;

int main(){
    int n;cin>>n;
    vector<int> a(n,0);
    rep(i,n)cin>>a[i];
    vector<ll> sum(n,0),max_v(n,0);
    sum[0] = a[0];
    max_v[0] = max(0,a[0]);
    for(int i = 1;i < n;i++){
        sum[i] = sum[i-1] + a[i];
        max_v[i] = max(sum[i],max_v[i-1]);
    }
    ll ans = 0,cal = 0;
    rep(i,n){
        ans = max(cal + max_v[i],ans);
        cal += sum[i];
    }
    cout<<ans<<endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size() 
#define all(v) v.begin(), v.end()
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)


int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int n; cin >> n;
    vector <int> A(n);
    for (int &i: A) cin >> i;
    ll mx = 0, curr = 0, ans = -1e18, p = 0;
    for (int i = 0; i < n; ++i) {
        ans = max(ans, curr + mx);
        curr += p + A[i];
        p += A[i];
        mx = max(mx, p);
    }

    cout << max(curr, ans);

    return 0;
}

