#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for(int i = 0; i < n; ++i)
using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main()
{
	int N; cin >> N;
	vector<LLONG> as(N); REP(i, N) cin >> as[i];
	vector<LLONG> tmps(N);
	LLONG sum = 0;
	REP(i, N)
	{
		sum += as[i];
		tmps[i] = sum;
	}
	vector<LLONG> sums(N);
	sum = 0;
	REP(i, N)
	{
		sum += tmps[i];
		sums[i] = sum;
	}
	int pos = distance(sums.begin(),
		max_element(sums.begin(), sums.end()));
	sum = sums[pos] - tmps[pos];
	LLONG ans = 0;
	REP(i, pos + 1)
	{
		sum += as[i];
		ans = max(ans, sum);
	}
	if (pos < N - 1)
	{
		REP(i, pos + 2)
		{
			sum += as[i];
			ans = max(ans, sum);
		}
	}
	cout << ans << endl;
}

// AtCoder :: Beginner Contest 182 :: D - Wandering 
// https://atcoder.jp/contests/abc182/tasks/abc182_d

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<ll> A;
    A.reserve(N);
    copy_n(istream_iterator<ll>(cin), N, back_inserter(A));
    vector<ll> P(N, 0);
    partial_sum(A.begin(), A.end(), P.begin());
    ll posn{0};
    ll soln{0};
    ll max_advance{0};
    for (auto p : P) {
        max_advance = max(max_advance, p);
        soln = max(soln, posn + max_advance);
        posn += p;
    }
    cout << soln << "\n";
    return EXIT_SUCCESS;
}
