#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define fastIO std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0)
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

const int N = 2e5 + 10; 

int n, a[N];
ll s[N], ss[N], ms[N];

// a1  a1 + a2  a1 + a2 + a3

// a1 2*a1 + a2  3*a1 + 2*a2 + a3
// 2   3          2 


// -2  -3  -1  0  0  

int main()
{
	fastIO;
	cin >> n;
	rep (i,1,n + 1) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
		ss[i] = ss[i - 1] + s[i];
		ms[i] = max(ms[i - 1], s[i]);
	}

	ll ans = 0;

	rep (i,0,n) {
		ans = max(ans, ss[i] + ms[i + 1]);
	}

	cout << ans << endl;
	return 0;
}
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <climits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;

ull gcd (ull a, ull b) {
  if(b == 0) return a;

  return gcd(b, a % b);
}

ull lcm (ull a, ull b){
  // バッファ溢れを抑えるために順番大事！！！
  return a / gcd(a, b) * b;
}

ull calcDigitLength(ull n, int count) {
  if(n < 10) return count + 1;

  return calcDigitLength(n/10, count+1);
}

bool isPrime (ull a) {
  if(a == 0 || a == 1) return false;
  if(a == 2) return true;
  if(a == 3) return true;

  for(int i = 2; i * i <= a; i++) {
    if(a % i == 0) {
      return false;
    }
  }

  return true;
}

int main () {

  ll n;

  cin >> n;

  ll a;

  // 距離
  vector<ll> vec;

  // 前回進んだ距離
  vector<ll> susuda;
  ll sum = 0;

  ll currentMax = 0;
  ll currentPoint = 0;
  ll currentSumMax = 0;

  for(ll i = 0; i < n; i++) {
    cin >> a;
    sum += a;

    vec.push_back(a);
    susuda.push_back(sum);

    currentSumMax = max(sum, currentSumMax);

    currentMax = max(max(currentMax, currentPoint + sum), currentPoint +currentSumMax);
    currentPoint += sum;
  }

  cout << currentMax << endl;

}
