#include<bits/stdc++.h>
using namespace std;
#define int long long
void sol(){
int n;
cin >> n;
int a[n];
for(int &i:a) cin >> i;
int l[n], r[n];
l[0] = a[0];
r[n - 1] = a[n - 1];
for(int i = 1; i < n; i++) l[i] = max(l[i - 1] + a[i], a[i]);
for(int i = n - 2; i >= 0; i--) r[i] = max(r[i + 1] + a[i], a[i]);
int ans = -1e9;
for(int i = 0; i < n; i++) ans = max(ans, l[i]);
for(int i = 0; i < n; i++) ans = max(ans, r[i]);
for(int i = 1; i < n - 1; i++) ans = max(ans, r[i + 1] + l[i - 1]);
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--){
sol();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBpbnQgbG9uZyBsb25nCnZvaWQgc29sKCl7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgaW50IGFbbl07CiAgICBmb3IoaW50ICZpOmEpIGNpbiA+PiBpOwogICAgaW50IGxbbl0sIHJbbl07CiAgICBsWzBdID0gYVswXTsKICAgIHJbbiAtIDFdID0gYVtuIC0gMV07CiAgICBmb3IoaW50IGkgPSAxOyBpIDwgbjsgaSsrKSBsW2ldID0gbWF4KGxbaSAtIDFdICsgYVtpXSwgYVtpXSk7CiAgICBmb3IoaW50IGkgPSBuIC0gMjsgaSA+PSAwOyBpLS0pIHJbaV0gPSBtYXgocltpICsgMV0gKyBhW2ldLCBhW2ldKTsKICAgIGludCBhbnMgPSAtMWU5OwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgYW5zID0gbWF4KGFucywgbFtpXSk7CiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSBhbnMgPSBtYXgoYW5zLCByW2ldKTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPCBuIC0gMTsgaSsrKSBhbnMgPSBtYXgoYW5zLCByW2kgKyAxXSArIGxbaSAtIDFdKTsKICAgIGNvdXQgPDwgYW5zIDw8ICJcbiI7Cn0Kc2lnbmVkIG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKICAgIGNvdXQudGllKE5VTEwpOwogICAgaW50IHQgPSAxOwogICAvLyBjaW4gPj4gdDsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgc29sKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==