#include <bits/stdc++.h>
using namespace std;

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

template <class Monoid> struct SegmentTree{
    using Func = function<Monoid(Monoid, Monoid)>;

    int size;
    vector<Monoid> seg;
    const Func merge;
    const Monoid def;

    SegmentTree(int n, Monoid d, const Func f): merge(f), def(d){
        for(size = 1; size <= n; size <<= 1);
        seg.assign(size*2, def);
    }

    Monoid& operator[] (int i) { return seg[i+size]; };
    void build(){ for(int i = size-1;i >= 0;i--) seg[i] = merge(seg[i*2], seg[i*2+1]); } 

    void update(int i, Monoid val){
        seg[i += size] = val;
        while(i >>= 1) seg[i] = merge(seg[i*2], seg[i*2+1]);
    }

    void add(int i, Monoid val){ update(i, val+seg[i+size]); }

    Monoid get(int a, int b){
        Monoid l = def, r = def;
        for(a += size, b += size; a < b; a >>= 1, b >>= 1){
            if(a & 1) l = merge(l, seg[a++]);
            if(b & 1) r = merge(seg[--b], r);
        }
        return merge(l, r);
    }
};

int solve(int n);

struct Data{
	int l;
	int r;
	int sum;
	int ma;
	bool t;
	Data(): l(0), r(0), sum(0), ma(0), t(true){}
	Data(int a){
		l = a;
		r = a;
		sum = a;
		ma = a;
		t = false;
	}

};


signed main(){
	int n;

	while(cin >> n, n != 0){
		solve(n);
	}

	return 0;
}

int solve(int n){

	auto merge = [](Data a, Data b){
		if(b.t) return a;
		if(a.t) return b;
		Data c;
		c.l = max(a.l, a.sum + b.l);
		c.r = max(b.r, b.sum + a.r);
		c.sum = a.sum + b.sum;
		c.ma = max({a.ma, b.ma, a.r + b.l});
		c.t = false;
		return c;
	};

	SegmentTree<Data> seg(n, Data(), merge);

	for(int i = 0;i < n;i++){
		int a;
		cin >> a;
		seg[i] = Data(a);
	}
	seg.build();
	cout << seg.get(0, n).ma << endl; 

	return 0;
}

