#include<bits/stdc++.h>

using namespace std;

#define int long long
#define pii pair<int,int>

const int MaxN=100*1000+10;
int arr[MaxN];
int ps[MaxN];

int findRightSticky(int s,int e){
	int mx=arr[e];
	int ind=e;
	for (int i=s;i<=e;++i)
		if (mx<ps[e]-ps[i-1]){
			mx=ps[e]-ps[i-1];
			ind=i;
		}
	return ind;
}

int findLeftSticky(int s,int e){
	int mx=arr[s];
	int ind=s;
	for (int i=s;i<=e;++i)
		if (mx<ps[i]-ps[s-1]){
			mx=ps[i]-ps[s-1];
			ind=i;
		}
	return ind;
}

pair<int,int> findMaxSubarray(int s,int e){
	if (s==e)
		return make_pair(s,s);
	int mid=(s+e)/2;
	pii mxsubleft=findMaxSubarray(s,mid);
	pii mxsubright=findMaxSubarray(mid+1,e);
	int mxstickyleft=findRightSticky(s,mid);
	int mxstickyright=findLeftSticky(mid+1,e);
	int totalmx=max(ps[mxstickyright]-ps[mxstickyleft-1],max(ps[mxsubleft.second]-ps[mxsubleft.first-1],ps[mxsubright.second]-ps[mxsubright.first-1]));
	if (totalmx==ps[mxsubleft.second]-ps[mxsubleft.first-1])
		return mxsubleft;
	else if (totalmx==ps[mxsubright.second]-ps[mxsubright.first-1])
		return mxsubright;
	else
		return make_pair(mxstickyleft,mxstickyright);
}

int32_t main(){
	int n;
	cin >> n;
	while(n){
		for (int i=1;i<=n;++i){
			cin >> arr[i];
			ps[i]=ps[i-1]+arr[i];
		}
		pii prd=findMaxSubarray(1,n);
		cout << ps[prd.second]-ps[prd.first-1] << endl;
		cin >> n;
	}
	// cout << prd.first-1 << " " << prd.second-1 << endl;
}
