#include<iostream>
using namespace std;
int main() {
	int n;
	int a[5000];
	while (cin >> n&&n != 0) {
		int sum3 = 0;
		for (int i = 0; i<n; i++)
			cin >> a[i];
		for (int i = 0; i<n; i++) {
			int sum = 0;
			int sum2 = 0;
			if(a[i] < 0) goto yo;
			for (int j = 0; j <= i; j++) {
				if (a[j]<0||(j<=1&&a[j-1]<0&&(a[j-1]+a[j])<0)) goto stop;;
				sum2 = 0;
				for (int z = j; z <= i; z++) {
					sum2 += a[z];
					if (sum<sum2) sum = sum2;
				}
			stop:;
			}
			yo:;
			if (sum3<sum) sum3 = sum;
		}
		cout << sum3 << endl;
	}
}