#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
ll a[5000];


int main(){
	while(cin>>n,n!=0){
		int c=0;
		ll MAX=-10000000;
		cin>>a[0];
		rep(i,n-1){
			int t;
			cin>>t;
			if(a[c]*t<0){
				a[++c]=t;
			}
			else a[c]+=t;
		}
		
		rep(i,c+1){
			ll max1=-100000000,max2=-100000000;
			ll t=0;
			for(int j=i-1;j>-1;j--){
				t+=a[j];
				if(max1<t){
					max1=t;
				}
			}
			if(max1==-100000000) max1=0;
			t=0;
			for(int j=i+1;j<c+1;j++){
				t+=a[j];
				if(max2<t){
					max2=t;
				}
			}
			if(max2==-100000000) max2=0;
			if(max1<0) max1=0;
			if(max2<0) max2=0;
			//cout<<max1<<" "<<max2<<endl;
			if(MAX<a[i]+max1+max2) MAX=a[i]+max1+max2;
			//cout<<endl;
		}
		cout<<MAX<<endl;
		
		//rep(i,c+1) cout<<a[i]<<endl;
		//cout<<endl;
	}
	
	return 0;
}
			