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

int main() {
	int N;
	while (cin >> N) {
		if (N == 0) return 0;
		int A[5000] = {}, DP[5010][2] = {}, MAX = -1000000;
		for (int i = 0; i < N; i++) cin >> A[i], MAX = max(MAX, A[i]);
		for (int i = 0; i < N; i++) {
			DP[i + 1][1] = max(DP[i][1] + A[i], DP[i + 1][1]);
			DP[i + 1][1] = max(A[i], DP[i + 1][1]);
			DP[i + 1][0] = max(DP[i][0], DP[i + 1][1]);
		}
		if (DP[N][0] != 0) cout << DP[N][0] << endl;
		else cout << MAX << endl;
	}
}
