Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func main() {
	sum := make([]int, 5001)
	for {
		n := nextInt()
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			a := nextInt()
			sum[i+1] = sum[i]+a
		}
		ret := sum[1]
		for i := 0; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				ret = max(ret, sum[j]-sum[i])
			}
		}
		fmt.Println(ret)
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int debug = 0;

int main(void){
	int n, m, i, j, k;
	int tmp, max;
	bool first = 0, notAllMinus=0;
	vector<int> intv;

	while(cin >> n){
		if(n == 0){
			break;
		}else{
			//ÇÝÝ
			for(i=0;i<n;i++){
				cin >> m;
				intv.push_back(m);
				//if(debug) cout << "int[" << i << "] =" << intv[i] << endl; 
			}

			//SÌêÌ»è
			tmp = intv[0];
			for(i=0;i<intv.size();i++){
				if(intv[i] > 0){
					notAllMinus = 1;
					break;
				}else{
					if(tmp < intv[i]){
						tmp = intv[i];
					}
				}
			}

			if(notAllMinus == 0){
				cout << tmp << endl;
				continue;
			}

			//vfð¸ç·
			tmp=0;
			for(i=1;i<intv.size();i++){
				if(intv[tmp] > 0){
					if(intv[i] > 0){ //¯Èç
						intv[tmp] += intv[i];
						intv[i] = 0;
					}else{ //ÙÈç
						tmp = i;
					}
				}else if(intv[tmp] < 0){
					if(intv[i] < 0){
						intv[tmp] += intv[i];
						intv[i] = 0;
					}else{
						tmp = i;
					}
				}
			}
			/*
			if(debug){
				for(i=0;i<intv.size();i++){
					cout << intv[i] <<endl;
				}
				cout << "----------------------" << endl;
			}
			*/
			intv.erase( remove(intv.begin(), intv.end(), 0), intv.end());
			/*
			if(debug){
				for(i=0;i<intv.size();i++){
					cout << intv[i] <<endl;
				}
			}
			*/

			int vsize = intv.size();
			for(i=1;i<=vsize;i+=2){//i:Â
				//if(debug) cout << "number =" << i << endl;
				for(j=0;j<=vsize-1-i+1;j++){ //j:JnÊu
					tmp = 0;
					for(k=0;k<i;k++){
						tmp += intv[j+k];
					}
					//if(debug) cout << "tmp ==" << tmp << endl;
					if(first == false){
						max = tmp;
						first = true;
					}else if(max < tmp){
						max = tmp;
					}
				}
			}
			cout << max << endl;
			intv.clear();
			first = false;
			notAllMinus = 0;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n;
int a[50001];
int S[50001];


int main(){

while(cin >> n && n > 0){
  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
  }

  int sum = 0; S[0] = 0;
  for (int i = 0; i < n ; ++i)
  {
    sum += a[i];
    S[i + 1] = sum;
    
  }

  int max = 0;
  for (int i = 0; i < n + 1; ++i)
  {
    for (int k = 0; k < i + 1; ++k)
    {
      if(S[i] - S[k] > max) max = S[i] - S[k];
    }
  }

  cout << max << endl;

  }
}

C++: #include<iostream>
#include<vector>

using namespace std;

#define N 2 << 20;

int main(){

  int n;


  while(cin >> n){
    if(n == 0) break;
    vector<int> V;
    int status;
    int in;
    int sum = 0;
    for(int i = 0; i < n; i++){
      cin >> in;
      if(i == 0){
	if(in < 0) status = -1;
	else if(in > 0)status = 1;
	else{
	  V.push_back(0);
	  status = 0;
	}


      } else {
	if(in == 0) {
	  V.push_back(sum);
	  sum = 0;
	  V.push_back(0);
	}

	if(in < 0 && status == 1 || in < 0 && status == 0){
	  status = -1;
	  V.push_back(sum);
	  sum = 0;
	}else if(in > 0 && status == -1 || in > 0 && status == 0){
	  status = 1;
	  V.push_back(sum);
	  sum = 0;
	}
      }
      sum += in;
    }

    V.push_back(sum);

    int max = -1*N;
    
    for(int i = 0; i < V.size(); i++){
      sum = 0;
      for(int j = i; j < V.size(); j++){
	sum += V[j];
	if(max < sum) max = sum;
      }
    }
    
    cout << max << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
using namespace std;

int main(void){
	long ans=0,part=0;
	int n,i,data;

	cin>>n;
	while(n!=0){
		for(i=0;i<n;i++){
			cin>>data;
			if(i==0){
				ans=data;
				part=data;
			}
			else{
				part+=data;
				if(part<0)
					part=0;
				else{
					if(ans<part)
						ans=part;
				}
			}
		}
		cout<<ans<<endl;
		cin>>n;
	}

	return 0;
}

C++: #include <iostream>
#include <climits>
#include <numeric>
int main()
{
  while (1) {
    int n;
    std::cin >> n;
    if (n == 0) break;
    int max = -INT_MAX;
    int a[n];
    for (int i = 0;i < n; i++) {
      std::cin >> a[i];
    }
    for (int i  = 0; i < n; i++) {
      if (a[i] < 0) continue;
      for (int j = i + 1; j <= n; j++) {
        int temp = std::accumulate(a + i, a + j, 0);
        if (temp > max) max = temp;
      }
    }
    std::cout << max << std::endl;
  }
}

C++: #include <stdio.h>
int array[1000];
int n,i,sum=0,k=0;
void tansaku(int a);
int main(void)
{
	while(1)
	{
		sum = 0;
		for(i=0;i<1000;i++)
		{
			array[i] = 0;
		}
		scanf("%d",&n);
		if(n==0)break;
		for(i=0;i<n;i++)
		{
			scanf("%d",&array[i]);
		}
		for(i=0;i<n;i++)
		{
			tansaku(i);
			k = 0;
		}
		printf("%d\n",sum);
	}
	return 0;
}
void tansaku(int a)
{
	if(a>n)return;
	k += array[a];
	if(k > sum)
	{
		sum = k;
	}
	if(a == n)return;
	tansaku(a+1);
}

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

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

int main()
{
    int n;
    while (cin >> n, n)
    {
        vector<long long int> data;
        data.push_back(0);
        rep(i, n)
        {
            long long int temp;
            cin >> temp;
            data.push_back(data[i] + temp);
        }
        long long int ans = 0;
        rep(i, n+1)
        {
            REP(j, i+1, n+1)
            {
                ans = max(ans, data[j] - data[i]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
using namespace std;

int main(void){
	long ans=0,part=0;
	int n,i,data;

	cin>>n;
	while(n!=0){
		for(i=0;i<n;i++){
			cin>>data;
			if(i==0){
					ans=data;
					if(data>0)
						part+=data;
			}
			else{
				part+=data;
				if(part<0)
					part=0;
				else{
					if(ans<part)
						ans=part;
				}
			}
		}
		cout<<ans<<endl;
		part=0;
		cin>>n;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif


void solve() {
    while (true) {
        ll N;
        cin >> N;
        if (N == 0) break;
        vector<ll> A(N);
        rep(i, N) cin >> A[i];
        vector<ll> dp(N + 1, -INF);
        rep(i, N) {
            dp[i + 1] = max(dp[i] + A[i], A[i]);
        }
        ll ans = *max_element(dp.begin(), dp.end());
        cout << ans << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}