#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007 // 998244353
#define INFLL (1LL<<62)
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
__attribute__((constructor))
void init(){ std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout<<std::fixed<<std::setprecision(15); }
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

int main(){
    ll n, ans = 0;
    cin >> n;
    vi a(n), sa(n), ssa(n), sssa(n, 0);
    map<ll, ll> cnt;
    rep(i, n){
        cin >> a[i];
        cnt[(a[i] == 0 ? 0 : (a[i] <= -1 ? -1 : 1))]++;
        sa[i] = a[i];
        if(i != 0){
            sa[i] += sa[i-1];
            ssa[i] += ssa[i-1] + sa[i];
            chmax(sssa[i], max(sssa[i-1], sa[i]));
        }
        else{
            ssa[i] = a[i];
            chmax(sssa[i], sa[i]);
        }
    }

    // 全部合算でOK
    if(cnt[1]+cnt[0] == n){
        cout << ssa[n-1] << endl;
        return 0;
    }
    else if(cnt[0] + cnt[-1] == n){
        cout << 0 << endl;
        return 0;
    }

    rep(i, n-1){
        chmax(ans, ssa[i]);
        chmax(ans, ssa[i] + sssa[i]);
    }
    chmax(ans, ssa[n-1]);
    cout << ans << endl;
}
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")

//# include <x86intrin.h>
# include <bits/stdc++.h>

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
 
template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red

void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
	// cin.exceptions(cin.failbit); 
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9; 
const int N = 6e5+5;                                         
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
 
int rnd (int l, int r) {
	return uniform_int_distribution<int> (l, r)(gen);
}

void solve() {
	int n;
	cin >> n;
	ll sum = 0;
	ll cur = 0;
	ll ans = 0, pref = 0;
	for (int i = 0; i < n; ++i) {
		int x;
		cin >> x;
		sum += x;
		pref = max(pref, sum);
		ans = max(ans, cur+pref);
		cur += sum;
	}
	cout << ans << '\n';
}

int main () {
	SpeedForce;
	
	int T = 1;
	//cin >> T;
	while(T--) solve();
	
	return Accepted;
}

// B...a
