//
//AOJ volume0 0022
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0022&lang=jp
//20170529
//640009E
//Yang Bo
//

#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> a(n, 0); //?´??????¨
    vector<int> b(n, 0); //????????¨
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int k = n - 1;
    while (a[k] < 0)
      k--;
    b[k] = a[k];
    for (int i = k - 1; i > -1; i--) {
      if (a[i] < 0)
	continue;
      else {
	int j = i + 1;
	int s = 0; //?????????????????????????????¨
	while (a[j] < 0) {
	  s += a[j];
	  j++;
	}
	//cout << "s = " << s << " " << "i = " << i << " " << "j = " << j << endl;
	if (s + b[j] >= 0)
	  b[i] += s + b[j] + a[i];
	else // if (a[i] > s + b[j])
	  b[i] += a[i];
	//cout << "b[" << i << "]= " << b[i] << endl;
      }
    }
    /*
    for (int i = 0; i < n; i++)
      cout << "b[" << i << "]= " << b[i] << endl;
    */
    int max = b[0];
    for (int i = 1; i < n; i++)
      if (max < b[i])
	max = b[i];
    if (max < 1) {
      int minus = a[0];
      for (int i = 1; i < n; i++)
	if (minus < a[i])
	  minus = a[i];
      max = minus;
    }
    cout << max << endl;
  }
}

/*
??????????????????????????£???????????§??????????????????wrong answer???????????§??????
????????§?????£???????????¨???????????§?????????????????????????????????
?????????????????§????????§????±?????????????(??????b?????\????????????????????????)?????¨????????°?????????????±?????????¨????????¢?????´?????????????????£???????????????
????????£???????¬??????????????¨?????????¨?¨????????????§???????????????
*/