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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> P;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

void YN(bool flg){if(flg) cout << "YES" << endl; else cout << "NO" << endl;}
void Yn(bool flg){if(flg) cout << "Yes" << endl; else cout << "No" << endl;}
void yn(bool flg){if(flg) cout << "yes" << endl; else cout << "no" << endl;}

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

const ll MOD = 1e9 + 7;

int main()
{   
    int n;
    cin >> n;
    vll a(n);
    rep(i, n) cin >> a[i];

    vll x(n, 0);
    x[0] = a[0];
    for(int i = 1; i < n; i++) x[i] = x[i-1] + a[i];

    vll maxx(n, 0);
    ll mx = 0;
    rep(i, n) {
        maxx[i] = max(mx, x[i]);
        chmax(mx, x[i]);
    }
    
    vll p(n+1, 0);
    rep(i, n) p[i+1]=p[i]+x[i];

    ll ans = 0;
    for(int i = 0; i < n; i++) chmax(ans, p[i]+maxx[i]);
    
    cout << ans << endl;

    //for(int t:maxx) cout << t << endl;

    return 0;
}
