#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define int long long int
#define ld long double
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'	
#define setbits(x) (__builtin_popcount(x))
int mod=1000000007;
int INF64=mod*mod+1;

void solve(){
	int n;
	cin>>n;
	vector<int> a(n);

	for(int i=0;i<n;i++)
		cin>>a[i];

	int curr=0,pref=0,maxpref=0,ans=0;
	for(int i=0;i<n-1;i++){
		pref+=a[i];
		curr+=pref;
		ans=max(ans,curr+maxpref);
		maxpref=max(maxpref,pref);
	}
	ans=max(ans,curr+pref+a[n-1]);

	cout<<ans<<endl;
}

int32_t main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int T=1;
	// cin>>T;
	for(int i=1;i<=T;i++){
		// cout<<"Case #"<<i<<": ";
		solve();
	}
	cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
}
#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
	int i,all = 1;
	cin >> n;
	while (all < n) all <<= 1;
	all -= 1;
	for (i = 0; i < n; ++i){
		int lc = all&(i<<1),rc = all&(i<<1|1);
		if (lc >= n) lc -= (all+1)>>1;
		if (rc >= n) rc -= (all+1)>>1;
		++lc,++rc;
		cout << lc << ' ' << rc << '\n'; 
	}
	return 0;
}