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 <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[6000];

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=-1000000000000000000;
		rep(i,n) a[i]=0;
		
		int cou=0;
		rep(i,n){
			cou++;
			cin>>a[0];
			if(a[0]!=0){
				break;
			}
			
		}
		n-=cou;
		rep(i,n){
			int t;
			cin>>t;
			if(a[c]*t<0){
				a[++c]=t;
			}
			else a[c]+=t;
		}
		
		
		c++;
		//rep(i,c) cout<<i<<" "<<a[i]<<endl;
		
		//rep(i,c) cin>>a[i];
		MAX=a[0];
		rep(i,c){
			ll sum=0;
			for(int j=i;j<c;j++){
				sum+=a[j];
				MAX=max(MAX,sum);
			}
		}
		
		
		
		/*
		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>
#include<algorithm>

int main(){

	int n;
	while (std::cin >> n, n){

		int max = 0, before;
		std::cin >> before;

		for (int i = 1, a; i < n; i++){
			std::cin >> a;
			before = std::max(before + a, a);
			max = std::max(max, before);
		}

		std::cout << max << std::endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	long int n, m[5001] = { 0 }, max = -2147483647, temp = 0, k;
	while (cin>> n && n!=0){
		for (int i = 0; i < n; i++){
			cin >> m[i];
		}
		for (int i = 0; i < n; i++){
			temp = m[i];
			for (int l = i + 1; l < n; l++){
				temp += m[l];
				if (temp > m[i]) if (max < temp) max = temp;
				else if (max < m[i]) max = m[i];
			}temp = 0;
		}
		cout << max << endl;
		max = -2147483647;
	}
	return 0;
}

C++: #include<iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<sstream>
#include<algorithm>
#include<map>
#include<complex>
using namespace std;


#define li			long long int
#define rep(i,to)	for(int i=0;i<((int)(to));i++)
#define repp(i,start,to)	for(int i=(int)(start);i<((int)(to));i++)
#define pb			push_back
#define sz(v)		((li)(v).size())
#define bgn(v)		((v).begin())
#define eend(v)		((v).end())
#define allof(v)	(v).begin(), (v).end()
#define dodp(v,n)		memset(v,(li)n,sizeof(v))
#define bit(n)		(1ll<<(li)(n))
#define mp(a,b)		make_pair(a,b)
#define rin	rep(i,n)
#define EPS 1e-10
#define ETOL 1e-8
#define MOD 1000000007
#define F first
#define S second
#define p2(a,b)		cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)		cout<<a<<"\t"<<b<<"\t"<<c<<endl

li a[10000], sum[10000];

int main(){
	li n;
	cin>>n;
	while(n>0){
		rin{cin>>a[i];}
		
		sum[0]=0;
		rin{
			sum[i+1]=a[i]+sum[i];
		}
		li res=-100000*5005;
		
		rep(i,n){
			repp(j,i+1,n+1){
				res=max(res,sum[j]-sum[i]);
			}
		}
		cout<<res<<endl;
		
		cin>>n;
	}
	
	return 0;
}

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

int main() {
	int n;
	while (true) {
		cin >> n;
		if (n == 0)break;
		int* nums = new int[n];
		for (int i = 0; i < n; i++) {
			cin >> nums[i];
		}
		long int max = -100001; 
		for (int i = 0; i < n; i++) {
			long int tmp = 0;
			for (int j = i; j < n ; j++) {
				tmp += nums[j];
				if (max < tmp)max = tmp;
			}
		}
		cout << max << endl;
	}
}

C++: #include<iostream>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
	long long int n,cnt = 0,ans = 0;
	stack<int> st;

	while(1){
		cin >> n;
		if(n == 0)break;

		long long int a[n];

		ans = 0;

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

		st.push(ans);
		cnt++;
	}

	for(int i = 0;i < cnt;i++){
		cout << st.top() << endl;
	}
}

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

int main()
{
	int n;
	while(cin>>n,n)
	{
		int data[n],dp[n+1];
		int tmp=0,ans;
		for (int i = 0; i < n; ++i)
		{
			cin>>data[i];
		}
		dp[0]=data[0];
		for (int i = 1; i < n; ++i)
		{
			dp[i]=max(dp[i-1]+data[i],data[i]);
		}
		sort(dp,dp+n);
		cout<<dp[n-1]<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define BE(c) c.begin(),c.end()
#define PB push_back
#define F first
#define S second
#define BEB(s) s
using namespace std;
int main(){
	int n,d[5001];
	while(1){
		cin>>n;
		if(n==0)break;
		d[0]=0;
		FOR(i,1,n+1){
			int a;
			cin>>a;
			d[i]=d[i-1]+a;
		}
		int ans=0;
		FOR(i,1,n+1)REP(j,i)ans=max(ans,d[i]-d[j]);
		cout<<ans<<endl;
	}
	return 0;
}

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

int n,s[5000+10];

int bigger(int n){
    int M=-100000;
    for(int p=1;p<n;p++){
        if(s[p]>M) M=s[p];
        for(int q=0;q<p-1;q++){
            if(s[p]-s[q]>M) M=s[p]-s[q];
        }
    }
    return M;
}

int main(){
    int n;
    while(cin){
        cin >> n;
        if(n==0) break;
        int a[5000+10];
        for(int i=0;i<n;i++){
            cin >> a[i];
            if(i==0) s[i]=a[i];
            else s[i]=s[i-1]+a[i];
        }
        cout << bigger(n) << endl;
    }
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <algorithm>

using namespace std;


bool solve(){

	while(1){
		int n;
		cin>> n;
		if(!n) break;
		
		vector<int> a(n,0);
		for(int i=0;i<n;i++){
			cin>> a[i];
		}
		
		int sum=0;
		for(int i=0;i<n;i++){
			int sum2 = a[i];
			if(sum < sum2) sum = sum2;
			for(int j=i+1;j<n;j++){
				sum2 += a[j];
				if(sum < sum2) sum = sum2;
			}
		}
		
		cout<< sum<< endl;
	}
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	solve();

	return 0;
}

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


int main()
{
    for (int n; cin >> n && n;) {
        vector<int> sequence(n);
        int sum = 0;
        int min = 0;
        int min_buffer = 0;
        int max = numeric_limits<int>::min();
        for (int x, i = 0; i < n && cin >> x; i++) {
            sum += x;
            if (sum > max) {
                max = sum;
                min = min_buffer;
            } else if (sum < min)
                min_buffer = sum;
        }
        cout << max - min << endl;
    }

    return 0;
}

C++: #define _USE_MATH_DEFINES
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main(){
	int n;

	while(cin >> n){
		int max = 0;
		int soremade = 0;
		int mae = 0;
		int ima = 0;

		if(n==0) break;
		
		cin >> mae;
		max = mae;

		for(int i=1;i<n;i++){
			if(mae > soremade+mae){
				soremade = mae;
			}else{
				soremade += mae;
			}
			
			cin >> ima;
			
			if(max < ima){
				max = ima;
			}
			if(max < ima+soremade){
				max = ima+soremade;
			}
			mae = ima;
		}

		cout << max << endl;
	}

	return 0;
}