#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<stack>
#include<queue>
#include<deque>
#include<algorithm>
#include<cmath>
#include<functional>
#include<bitset>
#include<iomanip>
#define _GLIBCXX_DEBUG
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define rep(i,n) for(int i=0;i<n;++i)
#define rep2(i,l,r) for(int i=l;i<=r;++i)
#define rep3(i,l,r) for(int i=l;i>=r;--i)
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const long long inf = 1ll << 60;
const long long mod=1000000007;
const double PI = acos(-1);

int main(){
    int n;
    cin >> n;
    V<ll> a(n);
    for(int i=0;i<n;++i){cin >> a[i];}

    ll proc = 0,start = 0;
    ll mx = 0, ans = 0;
    for(int i=0;i<n;++i){
        proc += a[i];
        mx = max(mx, proc);
        ans = max(ans, start+mx);
        start += proc;
    }
    cout << ans << endl;
    return 0;
}
/*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE   freopen("input.txt", "r", stdin);  freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll t, n, a[N], res=0, sum;
ll pre[N], fp[N], nxt;
int solve() {
	in1(n);
	pre[0]=MIN;
	for (int i = 1; i <= n; i++) {
		in1(a[i]);
		sum += a[i];
		pre[i] = max(pre[i - 1], sum);
		fp[i] = sum;
		res=max(res, nxt+pre[i]);
		nxt += fp[i];
	}
	printf("%lld\n", res);

	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	return solve();
}