#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = long long;
using Graph = vector<vector<int>>;
const long long INF = 1LL << 60;
const int SINF = 1LL << 29;
const ll mod = 1000000000+7;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];

    ll max_place = 0, max_start = 0;
    ll place = 0, start = 0;
    ll ans = 0;
    rep(i, n) {
        place += a[i];
        chmax(max_place, place);
        chmax(ans, max_place + start);
        start += place;
    }
    cout << ans << endl;
}


#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;
    
}
