Go: package main

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

var infty = 100000 * 2
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() (int, error) {
	scanner.Scan()
	return strconv.Atoi(scanner.Text())
}

func findMaximumSubarray(arr []int) int {
	currentMaxSum, maxSumContainsTail := -infty, -infty

	for j := range arr {
		if arr[j] > maxSumContainsTail+arr[j] {
			maxSumContainsTail = arr[j]
		} else {
			maxSumContainsTail += arr[j]
		}
		if currentMaxSum < maxSumContainsTail {
			currentMaxSum = maxSumContainsTail
		}

		// fmt.Println(arr[j], currentMaxSum, maxSumContainsTail)
	}
	return currentMaxSum
}

func main() {
	scanner.Split(bufio.ScanWords)
	for {
		size, _ := nextInt()
		if size == 0 {
			break
		}

		arr := make([]int, size)
		for i := range arr {
			n, _ := nextInt()
			arr[i] = n
		}

		maximumSum := findMaximumSubarray(arr)
		fmt.Println(maximumSum)
	}
}

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(0);
	  sum = 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 <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
//#define scanf scanf_s

int main()
{
	int n;
	int a[5005];
	while (cin >> n, n)
	{
		memset(a, 5005, 0);
		REP(i, n) cin >> a[i];
		int s[5005];
		s[0] = 0;
		FOR(i, 1, n+1) s[i] = s[i - 1] + a[i-1];
		int ans = -INF;
		REP(i, n)
		{
			for (int j = i + 1; j < n+1; j++)
			{
				ans = max(ans, (s[j] - s[i]));
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

C++: #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
int a[5001];
int b[5001];
int s;
int main()
{
	while(cin >> n && n>0)
	{
		b[0]=0;
		cin >> a[1];
		b[1]=a[1];
		s=b[1];
		for(int i=2; i<=n; i++)
		{
			cin >> a[i];
			b[i]=b[i-1]+a[i];
			for(int j=0; j<i; j++)
			{
				s=max(s, b[i]-b[j]);
			}	
		}
		cout << s << endl;
	}
	return 0;
}

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

using namespace std;

int findMax(vector<int>& a) {
  int sum, max = 0;
  for (int i = 0; i < a.size(); i++) {
    for (int j = i, sum = 0; j < a.size(); j++) {
      sum += a[j];
      if (max < sum) max = sum;
    }
  }
  return max;
}

int main() {
  int n, t, max;
  while (1) {
    cin >> n;
    if (n == 0) break;
    vector<int> a;
    for (int i = 0; i < n; i++) {
      cin >> t;
      if (-100000 <= t && 100000 >= t)
        a.push_back(t);
    }
    cout << findMax(a) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;

#define ull unsigned long long
#define ll long long
#define rep(i,j) for(int i=0;i<j;++i)
#define loop(i,a,j) for(int i=a;i<j;++i)
#define mpi(it,table,T,E) for(map<T,E>::iterator it = table.begin();it!=table.end();++it)
#define PI 3.141592654
/*
ifstream fin("input.txt");
ofstream fout("output.txt");
#define cin fin
#define cout fout
*/
#define N 5005

int sol[N][N];
bool visited[N][N];
int data[N];

int solve(int i,int j,int sum){
	if( i == j) return sum;
	if(visited[i][j])return sol[i][j];
	
	int a = max(sum,solve(i+1,j,sum-data[i]));
	int b = max(sum,solve(i,j-1,sum-data[j]));
	sol[i][j] = max(a,b);

	visited[i][j] = true;
	return sol[i][j];
}


int main(){
	int n;
	while(scanf("%d",&n),n){
		memset(visited,0,sizeof(visited));
		memset(sol,0,sizeof(visited));
		int sum = 0;
		rep(i,n){
			scanf("%d",&data[i]);
			sum += data[i];
		}
		sum = solve(0,n-1,sum);
		printf("%d\n",sum);	
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;

int main(){
	int N;
	int x;
	while(1){cin>>N;if(N==0) break;int index=0;
		ll ans = INT_MIN;
		vl sum(N+1);
		FOR(i,1,N+1){
			cin>>sum[i];
			sum[i] += sum[i-1];
		}
		FOR(i,1,N+1){
			FOR(j,i,N+1){
				ans = max(ans,sum[j]-sum[i-2]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;
int sum[5001];
int main(){
	int N;
	int x;
	while(cin>>N&&N){
		int index=0;
		int ans = 0;
		
		FOR(i,1,N+1){
			cin>>sum[i];
			sum[i] += sum[i-1];
		}
		FOR(i,1,N+1){
			FOR(j,i,N+1){
				ans = max(ans,sum[j]-sum[i-2]);
			}
		}
		cout<< ans <<endl;
	}
}