#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 20, MAXM = 5e3 + 10, MOD = 1e9 + 7;

void solve() {
	int n;
	cin >> n;
	
	vector <long long> a(n), ps(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (i)
			ps[i] = ps[i - 1] + a[i];
		else
			ps[i] = a[i];
	}
		
	long long pos = 0;
	long long mx1 = 0;
	long long mx = 0;
	
	for (int i = 0; i < n; i++) {
		mx = max(mx, pos + mx1);
		pos += ps[i];
		mx1 = max(mx1, 1ll * ps[i]);
	}
	mx = max(mx, pos);
	cout << mx << endl;
}
 
int main() {
   	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   	solve();
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main()
{
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A.at(i);
    vector<ll> acc(N+1, 0);
    ll M = 0;
    ll p = 0;
    ll q = 0;
    vector<ll> max_acc(N+1, 0);
    rep(i, N){
        acc.at(i+1) = acc.at(i) + A.at(i);
    }
    rep(i, N){
        max_acc.at(i+1) = max(max_acc.at(i), acc.at(i+1));
    }
    ll ans = 0;
    ll t = 0;
    rep(i, N){
        chmax(ans, t+max_acc.at(i+1));
        chmax(ans, t+acc.at(i+1));
        t += acc.at(i+1);
    }
    cout << ans << endl;
}