/* AOJ 0022 Maximum Sum Sequence */

#include <iostream>
#include <climits>

using namespace std;

void update(int i, int x);
void init();

const int MAX_N = 5000;

int max_sum[MAX_N + 1];
int diff_from_max[MAX_N + 1];
int sum_last_non_negative[MAX_N + 1];

int
main()
{
    for (;;) {
        int n, i;

        cin >> n;
        if (n == 0) break;

        init();
        for (i = 1; i <= n; i++) {
            int a;

            cin >> a;
            update(i, a);
        }

        cout << max_sum[n] << endl;
    }

    return 0;
}

void
update(int i, int x)
{
    if (x < 0) {
        if (max_sum[i - 1] <= x) {
            max_sum[i] = x;
            diff_from_max[i] = x;
        } else {
            max_sum[i] = max_sum[i - 1];
            diff_from_max[i] = diff_from_max[i - 1] + x;
        }

        sum_last_non_negative[i] = -1;
    } else {
        int diff_from_max_temp = diff_from_max[i - 1] + x;

        if (sum_last_non_negative[i - 1] < 0)
            sum_last_non_negative[i] = x;
        else
            sum_last_non_negative[i] = sum_last_non_negative[i - 1] + x;

        if (diff_from_max_temp > 0) {
            // 最大値を更新する
            int cumulative_sum = max_sum[i - 1] + diff_from_max_temp;

            max_sum[i] = max(sum_last_non_negative[i], cumulative_sum);
            diff_from_max[i] = 0;
        } else {
            if (max_sum[i - 1] < sum_last_non_negative[i]) {
                // 最大値を更新する
                max_sum[i] = sum_last_non_negative[i];
                diff_from_max[i] = 0;
            } else {
                max_sum[i] = max_sum[i - 1];
                diff_from_max[i] = diff_from_max_temp;
            }
        }
    }
}

void
init()
{
    max_sum[0] = INT_MIN;
    sum_last_non_negative[0] = -1;
}