#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
const int maxN = 1e5;
int a[maxN];
ll recur(int l, int r){
if(l + 1 >= r) return a[l];
int mid = (l + r) / 2;
ll ans = max(recur(l, mid), recur(mid, r));
ll ansL = LONG_LONG_MIN, ansR = LONG_LONG_MIN;
ll now = 0;
for(int i = l; i < mid; i++){
now += a[i];
ansL = max(ansL, now);
}
now = 0;
for(int i = mid; i < r; i++){
now += a[i];
ansR = max(ansR, now);
}
ll sum = max(ansL, max(ansR, ansL + ansR));
ans = max(ans, sum);
cout << l << " " << r << " " << ansL << " " << ansR << endl;
return ans;
}
signed main(){
ios;
int n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
cout << recur(0, n) << endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW9zIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKSwgY2luLnRpZSgwKSwgY291dC50aWUoMCk7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmNvbnN0IGludCBtYXhOID0gMWU1OwppbnQgYVttYXhOXTsKCmxsIHJlY3VyKGludCBsLCBpbnQgcil7CiAgICBpZihsICsgMSA+PSByKSByZXR1cm4gYVtsXTsKICAgIAogICAgaW50IG1pZCA9IChsICsgcikgLyAyOwogICAgbGwgYW5zID0gbWF4KHJlY3VyKGwsIG1pZCksIHJlY3VyKG1pZCwgcikpOwogICAgbGwgYW5zTCA9IExPTkdfTE9OR19NSU4sIGFuc1IgPSBMT05HX0xPTkdfTUlOOwogICAgbGwgbm93ID0gMDsKICAgIGZvcihpbnQgaSA9IGw7IGkgPCBtaWQ7IGkrKyl7CiAgICAgICAgbm93ICs9IGFbaV07CiAgICAgICAgYW5zTCA9IG1heChhbnNMLCBub3cpOwogICAgfQogICAgbm93ID0gMDsKICAgIGZvcihpbnQgaSA9IG1pZDsgaSA8IHI7IGkrKyl7CiAgICAgICAgbm93ICs9IGFbaV07CiAgICAgICAgYW5zUiA9IG1heChhbnNSLCBub3cpOwogICAgfSAgIAogICAgbGwgc3VtID0gbWF4KGFuc0wsIG1heChhbnNSLCBhbnNMICsgYW5zUikpOwogICAgYW5zID0gbWF4KGFucywgc3VtKTsKICAgIGNvdXQgPDwgbCA8PCAiICIgPDwgciA8PCAiICIgPDwgYW5zTCA8PCAiICIgPDwgYW5zUiA8PCBlbmRsOwogICAgcmV0dXJuIGFuczsgCiAgICAKICAgIAogICAgCiAgICAKICAgIAp9CnNpZ25lZCBtYWluKCl7CiAgICBpb3M7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CiAgICBjb3V0IDw8IHJlY3VyKDAsIG4pIDw8IGVuZGw7Cn0=