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 <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;
}

C++: #include <iostream>

using namespace std;

int S[5000];

int main(){
	int n;
	while(cin >> n,n){
		S[0] = 0;
		int tmp,sum = 0;
		for(int i = 1; i <= n; i++){
			cin >> tmp;
			sum += tmp;
			S[i] = sum;
		}
		tmp = S[1] - S[0];
		for(int i = 0; i <= n; i++){
			for(int j = i + 1; j <= n; j++){
				if(tmp < S[j] - S[i]) tmp = S[j] - S[i];
			}
		}
		cout << tmp << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define INF LLONG_MAX-1
using namespace std;
typedef long long ll;

ll a[100001];
int main() {
	ll n;
	while (scanf("%lld", &n), n) {
		ll cnt = 0;
		for (ll i = 0; i < n; i++) {
			scanf("%lld", &a[i]);
			cnt += a[i];
		}
		ll Max = cnt;
		ll w = 0;
		for (ll i = 0; i < n; i++) {
			cnt -= a[i];
			if (Max < cnt) {
				w = i;
				Max = cnt;
			}
		}
		cnt = Max;
		for (ll i = n - 1; i > w; i--) {
			cnt -= a[i];
			Max = max(Max, cnt);
		}
		printf("%lld\n", Max);
	}
	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 = 0;
		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 <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

int main(){
	int n,a[5000],s,ans;
	
	while(1){
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> a[i] ;
		}
			if(n==0){
				break;
			}
		ans=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<=n-i;j++){
				s=0;
				if((a[j]<=0)||(a[j+i-1]<=0)){
					goto A;
				}
				for(int k=j;k<j+i;k++){
					s+=a[k];
				}
				if(ans<s){
					ans=s;
				}
					A:;
			}
		}
		if(ans==0){
			for(int i=1;i<n;i++){
				for(int j=0;j<n-i;j++){
					if(a[j]<a[j+1]){
						s=a[j]; a[j]=a[j+1]; a[j+1]=s;
					}
				}
			}
			ans=a[0];
		}
	cout << ans << "\n" ;
	}
return 0;
}