#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000001;
const long long INF = 1e18;
int n, a[MAXN];
long long prefSum[MAXN], prefMin[MAXN], ans = -INF;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
// Initializing prefix sum and prefix minimum
prefSum[0] = prefMin[0] = 0;
// Compute prefix sums and prefix minimums
for (int i = 1; i <= n; i++) {
prefSum[i] = prefSum[i - 1] + a[i];
prefMin[i] = min(prefMin[i - 1], prefSum[i]); // Store minimum prefix sum
}
// Finding the maximum subarray sum
for (int i = 1; i <= n; i++) {
ans = max(ans, prefSum[i] - prefMin[i - 1]);
// Maximum sum is found by subtracting the smallest prefix sum from the current prefix sum
}
cout << ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWE4gPSAxMDAwMDAxOwpjb25zdCBsb25nIGxvbmcgSU5GID0gMWUxODsKCmludCBuLCBhW01BWE5dOwpsb25nIGxvbmcgcHJlZlN1bVtNQVhOXSwgcHJlZk1pbltNQVhOXSwgYW5zID0gLUlORjsKCmludCBtYWluKCkgewogICAgY2luID4+IG47CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIGNpbiA+PiBhW2ldOwoKICAgIC8vIEluaXRpYWxpemluZyBwcmVmaXggc3VtIGFuZCBwcmVmaXggbWluaW11bQogICAgcHJlZlN1bVswXSA9IHByZWZNaW5bMF0gPSAwOwogICAgCiAgICAvLyBDb21wdXRlIHByZWZpeCBzdW1zIGFuZCBwcmVmaXggbWluaW11bXMKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIHByZWZTdW1baV0gPSBwcmVmU3VtW2kgLSAxXSArIGFbaV07CiAgICAgICAgcHJlZk1pbltpXSA9IG1pbihwcmVmTWluW2kgLSAxXSwgcHJlZlN1bVtpXSk7IC8vIFN0b3JlIG1pbmltdW0gcHJlZml4IHN1bQogICAgfQoKICAgIC8vIEZpbmRpbmcgdGhlIG1heGltdW0gc3ViYXJyYXkgc3VtCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICBhbnMgPSBtYXgoYW5zLCBwcmVmU3VtW2ldIC0gcHJlZk1pbltpIC0gMV0pOyAKICAgICAgICAvLyBNYXhpbXVtIHN1bSBpcyBmb3VuZCBieSBzdWJ0cmFjdGluZyB0aGUgc21hbGxlc3QgcHJlZml4IHN1bSBmcm9tIHRoZSBjdXJyZW50IHByZWZpeCBzdW0KICAgIH0KCiAgICBjb3V0IDw8IGFuczsKfQo=