/* 十 聖イシドールスよ、迷えるプログラマを導き給え！ 十 */
/* 重ならないように置く　でかい四角を縦線か横線で区切ったとき、2つの四角形にぶつからずに区切れるか? */
/* 1つのパターンで沢山区切れるので単純にやると重複が沢山出るが、
 * 片方の四角形を基準に線を引いてそれで考えればよい */
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>


const long long INF = 1LL << 60;
const long long MOD = 1000000007;
using ll = long long;
#define rep(i, n) for(int i = 0; i < n; i++)
#define MOD(n)   n=n%MOD;if(n < 0){n += MOD;}

int main()
{
	ll N;
	std::cin >> N;
	std::vector<ll> A(N, 0);

	ll ans = 0;
	ll zahyo = 0;

	ll all_mv = 0;
	ll max_point = 0;
	rep(i, N)
	{
		ll pre_all_mv = all_mv;
		ll now = 0;
		std::cin >> now;

		// 一番動いた位置 
		ll max = zahyo + max_point;
		ans = std::max(ans, max);
		
		all_mv += now;
		if(all_mv > max_point)
		{
			max_point = all_mv;
		}

		// 最後の位置 
		zahyo += pre_all_mv;
		zahyo += now;
		
		ans = std::max(ans, zahyo);
	}

	std::cout << ans;

	return 0;
}
#include<bits/stdc++.h>
#include<iomanip>
/*
#include<atcoder/all>
using namespace atcoder;
*/
using namespace std;
#define REP(i,n) for(int i = 0;i < n;i++)
#define RNG(i,s,n) for(int i = s;i <= n;i++)
#define _RNG(i,e,s) for(int i = e;i >= s;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define dup(x,y) (x + y - 1) / (y)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define lb(x,key) lower_bound(all(x) , (key))
#define ub(x,key) upper_bound(all(x) , (key))
#define _size(v) int((v).size())
#define prt(x) cout << x << '\n'
#define re return
#define re0 return 0
#define PQ(type) priority_queue<type>
#define PQD(type) priority_queue<type,vector<type>,greater<type>>

template<class T> bool chmax(T& a,T b){ if(a < b){ a = b; return true; }else return false; }
template<class T> bool chmin(T& a,T b){ if(a > b){ a = b; return true; }else return false; }

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
  cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
  return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
  cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
  return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
  cout << "{" << p.first << "," << p.second << "}" << '\n';
  return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
  for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
  cin >> p.first >> p.second; return in;
}

#define F first
#define S second
/********************************************************************************/
/* BE CAREFUL : OVERFLOW / MIN&MAX OF THE CONSTRAINTS / INDEX LABELING!! */

int main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);
  //cout << fixed << setprecision(20);
  
  int n;cin >> n;
  vl a(n);cin >> a;
  vl add(n),addmax(n);
  REP(i,n){
    if(i==0){
      add[i] = a[i];
      addmax[i] = a[i];
    }else{
      add[i] = add[i-1] + a[i];
      if(add[i] > addmax[i-1]){
        addmax[i] = add[i];
      }else{
        addmax[i] = addmax[i-1];
      }
    }
  }
  
  ll max_pos = 0;
  ll cur = 0;
  REP(i,n){
    chmax(max_pos,cur + addmax[i]);
    cur += add[i];
  }
  cout << max_pos << '\n';

  return 0;
}
