#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int Merge(vector<int>& A, int p)
{
	while (p > 1 && A[p] > 0){
		int a = A[p - 2] + A[p - 1] + A[p];
		if (a > A[p - 2] && a > A[p]){
			A[p - 2] += A[p - 1] + A[p];
			A[p - 1] = A[p] = 0;
			p -= 2;
		}
		else break;
	}
	return p;
}
int main()
{
	int n, k, p, m;
	bool isPlus = true;
	while (cin >> n){
		if (0 == n) break;
		vector<int> A(n, 0);
		m = INT_MIN;
		p = 0;
		for (int i = 0; i < n; ++i){
			cin >> k;
			if (m < k) m = k;
			if (0 == i){
				A[p] = k;
				isPlus = (k > 0);
			}
			else {
				if (isPlus == (k > 0)){
					A[p] += k;
				}
				else{
					if (isPlus){
						p = Merge(A, p);
						if (m < A[p]) m = A[p];
					}
					++p;
					A[p] = k;
					isPlus = !isPlus;
				}
			}
		}
		p = Merge(A, p);
		if (m < A[p]) m = A[p];
		cout << m << "\n";
	}
	return 0;
}