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<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(void){
    for(;;){
        int n;
        int a[10000]={0};
        cin>>n;
        if(n==0)break;
        int sum=0;
        int ans=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;i<n;i++){
            sum=0;
            for(int j=i;j<n;j++){
                sum+=a[j];
                ans=max(ans,sum);
            }
        }
        cout<<ans<<endl;

    }


	return 0;
}

C++: #include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000001
#define IN_MAX -9223372036854775808

using namespace std;
#define ll long long
#define dbg if(0)



int main(){
    while(true){
        int n;
        ll ans= IN_MAX;
        cin>>n;
        if (n==0)break;

        ll rui[n+1];
        rui[0]=0;
        for(int i=1;i<=n;i++){
            cin >>rui[i];
            rui[i]+=rui[i-1];
            dbg cout <<rui[i]<<" ";
        }
        //次を見る
        //今までの最小値を更新したか？
        //したなら暫定解を確定しansと比較。最大値=最小値=発見した最小値にする
        //してなければ最大値の更新をする
        //見る場所が一番後ろまで行ったら終了。
        ll nmin=rui[0],nmax=rui[0];
        for(int i=0;i<=n;i++){
            if (rui[i]<nmin){
                ans=max(nmax-nmin,ans);
                nmax=rui[i];
                nmin=rui[i];
            }else{
                if(rui[i]>nmax){
                    nmax=rui[i];
                }
            }
            dbg cout <<nmax<<" "<<nmin<<endl;
        }
        ans=max(nmax-nmin,ans);
        dbg cout << endl;
        cout<<ans<<endl;
    }

    return 0;
}

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

static const int MIN = -1 * (1 << 21);

int main() {
	int i, j, n, a, maxa, max_so_far, max_ending_here;
	
	while (1) {
		cin >> n;
		if (n == 0)break;
		max_so_far = 0;
		max_ending_here = 0;
		maxa = MIN;
		for (i = 0; i < n; i++) {
			cin >> a;
			maxa = max(maxa, a);
			max_ending_here = max_ending_here + a;
			if (max_ending_here < 0)max_ending_here = 0;
			if (max_so_far < max_ending_here)max_so_far = max_ending_here;
		}
		if (max_so_far == 0)max_so_far = maxa;
		cout << max_so_far << endl;

	}

	return 0;
}

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

using namespace std;

int main(void)
{
    ios::sync_with_stdio(false);
    while(1) {
        int n;
        int a[5001] = {0};
        long int ans = -10000000;
        cin >> n;
        if(n == 0) break;

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

        for(int i=0; i<n; i++) {
            long int sum = 0;
            for(int j=i; j<n; j++) {
                sum += a[j];
                ans = max(ans, sum);
            }
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <stack>
#include <cstring>

using namespace std;

int main(){
	int n;
	int max,tmpmax1,tmpmax2;
	while(cin >> n && n != 0){
		max = -100001;
		int *factor = new int[n];
		
		for(int i = 0; i < n; i++){
			cin >> factor[i];
		}

		for(int i = 0; i < n - 1; i++){
			tmpmax1 = 0;
			tmpmax2 = -100001;
			for(int j = i; j < n; j++){
				tmpmax1 += factor[j];
				if(tmpmax2 < tmpmax1){
					tmpmax2 = tmpmax1;
				}
			}
			if(tmpmax2 < 0){
				for(int i = 0; i < n; i++){
					if(tmpmax2 == factor[i]){
						if(i != n - 1 && factor[i + 1] < 0){
							tmpmax2 += factor[i + 1];
							break;
						}
						break;
					}
				}
			}
			if(max < tmpmax2){
				max = tmpmax2;
			}
		}
		delete [] factor;
		cout << max << endl;
	}
	
	return 0;
}

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

using namespace std;

int debug = 0;

int main(void){
	int i, j, k;
	int n, m;
	int tmp, maxi, cont;
	vector<int> intv, imax;

	while(cin >> n){
		if(n == 0) break;
		
		for(i=0;i<n;i++){
			cin >> m;
			intv.push_back(m);
		}

		cont = 0;
		maxi = 0;
		tmp = 0;
		for(i=0;i<intv.size();i++){
			cont = cont + intv[i];
			cont = max(0, cont);
			maxi = max(maxi, max(cont, intv[i]));
		}

		if(maxi != 0){
			cout << maxi << endl;
		}else{
			cout << *max_element(intv.begin(), intv.end()) << endl;
		}
		intv.clear();
	}

	return 0;
}

int b_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;
				intv.clear();
				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 <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
ll a[5000];

ll sum(int i,int j){
	ll s=0;
	rep(k,j){
		s+=a[i+k];
	}
	return s;
}

int main(){
	while(cin>>n,n!=0){
		int c=0;
		ll MAX=-100000000;
		rep(i,n) a[i]=0;
		int tmp=n;
		int cou=0;
		rep(i,tmp){
			cou++;
			cin>>a[0];
			//cout<<"a[0]= "<<a[0]<<endl;
			if(a[0]!=0){
				break;
			}
			
		}
		//cout<<"cou= "<<cou<<endl;
		n-=cou;
			
		/*
		rep(i,n-1) cin>>a[i+1];
		c=n-1;
		*/
		rep(i,n){
			int t;
			cin>>t;
			if(a[c]*t<0){
				a[++c]=t;
			}
			else a[c]+=t;
		}
		c++;
		for(int i=1;i<=c;i++){
			rep(j,c+1-i){
				MAX=max(MAX,sum(j,i));
			}
		}
		cout<<MAX<<endl;
	}
	return 0;
}