#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <queue>
using namespace std;

int main(void){
	int i,j,k,n,a[5000],ans;
	while(1){
	cin >> n;
	if(n == 0) break;
	ans = 0;
	a[0] = 0;
	k = 0;
	for(int t=0;t<n;t++){
		int x = 0;
		cin >> x;
		if(a[k] == 0) a[k] += x;
		else if(a[k] > 0 && x > 0 || a[k] < 0 && x < 0) a[k] += x;
		else a[++k] = x;
	}
	k++;
	if(a[0] < 0){
		for(i=0;i<k-1;i++) a[i] = a[i+1];
		k--;
	}

	for(i=1;i<k-1;i++){
		if(a[i] < 0 && a[i] + a[i+1] > 0){
			a[i-1] += a[i] + a[i+1];
			for(j=i;j<k-2;j++) a[j] = a[j+2];
			k -= 2;
		}
	}

	for(i=0;i<k;i++) ans = max(ans,a[i]);

	cout << ans << endl;
}
	return 0;
}