#include<iostream>
#define ll long long
using namespace std;
const int nmax = 1e6;
ll id[nmax + 5], lt[nmax + 5], rt[nmax + 5], s[nmax + 5], st[nmax + 5];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n; cin >> n;
ll a[n + 1], s[n + 1]; s[0] = 0;
int tmp = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
s[i] = s[i - 1] + a[i];
if(s[i] >= s[tmp]) tmp = i;
id[i] = tmp;
}
ll res = s[n];
lt[0] = rt[n + 1] = 1e18;
for(int i = 1; i <= n; i++){
ll tt;
tt = s[i] - s[id[i]];
lt[i] = min(lt[i - 1], tt);
}
tmp = n + 1;
for(int i = n; i >= 1; i--){
st[i] = st[i + 1] + a[i];
if(st[i] >= st[tmp]) tmp = i;
id[i] = tmp;
}
for(int i = n; i >= 1; i--){
ll tt;
tt = st[i] - st[id[i]];
rt[i] = min(rt[i + 1], tt);
}
for(int i = 1; i < n; i++){
res = max(res, s[n] - 2 * (lt[i] + rt[i + 1]));
}
cout << res;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNkZWZpbmUgbGwgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBubWF4ID0gMWU2OwpsbCBpZFtubWF4ICsgNV0sIGx0W25tYXggKyA1XSwgcnRbbm1heCArIDVdLCBzW25tYXggKyA1XSwgc3Rbbm1heCArIDVdOwppbnQgbWFpbigpewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7Y2luLnRpZSgwKTtjb3V0LnRpZSgwKTsKICAgIGludCBuOyBjaW4gPj4gbjsKICAgIGxsIGFbbiArIDFdLCBzW24gKyAxXTsgc1swXSA9IDA7CiAgICBpbnQgdG1wID0gMDsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKXsKICAgICAgICBjaW4gPj4gYVtpXTsKICAgICAgICBzW2ldID0gc1tpIC0gMV0gKyBhW2ldOwogICAgICAgIGlmKHNbaV0gPj0gc1t0bXBdKSB0bXAgPSBpOwogICAgICAgIGlkW2ldID0gdG1wOwogICAgfQogICAgbGwgcmVzID0gc1tuXTsKICAgIGx0WzBdID0gcnRbbiArIDFdID0gMWUxODsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKXsKICAgICAgICBsbCB0dDsKICAgICAgICB0dCA9IHNbaV0gLSBzW2lkW2ldXTsKICAgICAgICBsdFtpXSA9IG1pbihsdFtpIC0gMV0sIHR0KTsKICAgIH0KICAgIHRtcCA9IG4gKyAxOwogICAgZm9yKGludCBpID0gbjsgaSA+PSAxOyBpLS0pewogICAgICAgIHN0W2ldID0gc3RbaSArIDFdICsgYVtpXTsKICAgICAgICBpZihzdFtpXSA+PSBzdFt0bXBdKSB0bXAgPSBpOwogICAgICAgIGlkW2ldID0gdG1wOwogICAgfQogICAgZm9yKGludCBpID0gbjsgaSA+PSAxOyBpLS0pewogICAgICAgIGxsIHR0OwogICAgICAgIHR0ID0gc3RbaV0gLSBzdFtpZFtpXV07CiAgICAgICAgcnRbaV0gPSBtaW4ocnRbaSArIDFdLCB0dCk7CiAgICB9CiAgICBmb3IoaW50IGkgPSAxOyBpIDwgbjsgaSsrKXsKICAgICAgICByZXMgPSBtYXgocmVzLCBzW25dIC0gMiAqIChsdFtpXSArIHJ0W2kgKyAxXSkpOwogICAgfQogICAgY291dCA8PCByZXM7CiAJcmV0dXJuIDA7Cn0K