#include<iostream>
#include<vector>

int main() {
	int n;
	while (std::cin >> n) {
		if (n == 0) break;
		std::vector<int> a;
		int max = 0, left = 0, right = 0, lmin = 0, rmin = 0;
		for (int i = 0; i < n; ++i) {
			int ai;
			std::cin >> ai;
			a.push_back(ai);
			max += ai;
		}
		// ここをかく
		int lmax,rmax;
		std::vector<int>::iterator leftend = a.begin();
		std::vector<int>::reverse_iterator rightend = a.rbegin();
		for (std::vector<int>::iterator itr = a.begin(); itr != a.end(); ++itr) {
			left += *itr;
			if (left < lmin) {
				lmin = left;
				leftend = itr;
			}
		}
		for (std::vector<int>::iterator itr = a.end() - 1; itr != leftend; --itr) {
			right += *itr;
			if (right < rmin) {
				rmin = right;
			}
		}
		lmax = max - lmin - rmin;
		left = 0; right = 0; lmin = 0; rmin = 0;
		for (std::vector<int>::reverse_iterator ritr = a.rbegin(); ritr != a.rend(); ++ritr) {
			right += *ritr;
			if (right < rmin) {
				rmin = right;
				rightend = ritr;
			}
		}
		for (std::vector<int>::reverse_iterator ritr = a.rend() - 1; ritr != rightend; --ritr) {
			left += *ritr;
			if (left < lmin) {
				lmin = left;
			}
		}
		rmax = max - lmin - rmin;
		max = lmax > rmax ? lmax : rmax;

		std::cout << max << std::endl;
	}

	return 0;
}
