#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define el cout<<"\n"
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define f0(i,n) for(int i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<=n;i++)
#define fz(i,a,n,z) for(int i=a;i<n;i+=z)
#define rep(i,a,n,z) for(int i=a;i>n;i-=z)
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file(name) freopen(name".inp","r",stdin);freopen(name".out","w",stdout);
const int N = 2e5 + 5;
const int maxn = 1e6 + 1;
const int MOD = 1e9 + 7;
int main() {
faster
int n;
cin >> n;
ll dpLeft[n + 5], dpRight[n + 5], A[n + 5];
dpLeft[0] = dpRight[n + 1] = INT_MIN;
ll sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> A[i];
sum = max(A[i], A[i] + sum);
dpLeft[i] = max(dpLeft[i - 1], sum);
}
sum = 0;
for (int i = n; i >= 1; --i) {
sum = max(A[i], A[i] + sum);
dpRight[i] = max(dpRight[i + 1], sum);
}
ll res = INT_MIN;
for (int i = 1; i < n; ++i) {
res = max(res, dpLeft[i] + dpRight[i + 1]);
}
cout << res;
}
/*-----------------------END-----------------------*/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBmaSBmaXJzdAojZGVmaW5lIHNlIHNlY29uZAojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGVsIGNvdXQ8PCJcbiIKI2RlZmluZSBzeih4KSAoaW50KSh4KS5zaXplKCkKI2RlZmluZSBhbGwoeCkgKHgpLmJlZ2luKCksKHgpLmVuZCgpCiNkZWZpbmUgZjAoaSxuKSBmb3IoaW50IGk9MDtpPG47aSsrKQojZGVmaW5lIGYxKGksbikgZm9yKGludCBpPTE7aTw9bjtpKyspCiNkZWZpbmUgZnooaSxhLG4seikgZm9yKGludCBpPWE7aTxuO2krPXopCiNkZWZpbmUgcm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