#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v;
    int n, a, sum;
    while (cin >> n && n != 0) {
        v.clear();
        for (int i=0; i<n; ++i) {
            cin >> a;
            v.push_back(a);
        }

        int l = 0;
        sum = 0;
        for (int i=0; i<n; ++i) {
            sum += v[i];
            if (sum < 0) {
                sum = 0;
                l = i+1;
            }
        }

        int r = n-1;
        sum = 0;
        for (int i=n-1; i>=0; --i) {
            sum += v[i];
            if (sum < 0) {
                sum = 0;
                r = i-1;
            }
        }

        sum = 0;
        for (int i=l; i<=r; ++i) {
            sum += v[i];
        }

        cout << sum << endl;
    }
    return 0;
}