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 <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define EXIST(v,i) (find(all(v), i) != (v).end())
#define ERASE(v,i) (v).erase(remove(all(v), i), (v).end())
#define INDEX(v,i) (find(all(v), i) - (v).begin())
#define rep(i,n) for(int i = 0; i < (int)n; ++i)
#define debug(x) cerr << #x << " = " << (x) << endl;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

const int INF = 100000000;
const double PI = 3.14159265358979323846;
const double EPS = 1e-10;

int main() {
	for(int n; cin >> n && n;) {
		vector<int> arr(n);
		vector<int> cur(n);
		rep(i,n) cin >> arr[i];
		if(arr[0] > 0) cur[0] = arr[0];
		for(int i = 1; i < n; i++)
			cur[i] = max(arr[i], cur[i - 1] + arr[i]);
		int vmax = *max_element(all(cur));
		cout << (vmax == 0 ? *max_element(all(arr)) : vmax) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        int plus[5000] = {0}, max = -1000000, input;
        rep(i,n){
            cin >> input;
            plus[i]+= input;
            plus[i+1] = plus[i];
            if(max < plus[i]){
                max = plus[i];
            }
        }
        rep(i,n){
            rep(j,i-1){
                if(max < plus[i] - plus[j]){
                    max = plus[i] - plus[j];
                }
            }
        }
        cout << max << endl;
    }
}

C++: /*
	0022:Maximum Sum Sequence
*/
#include	<iostream>
#include	<algorithm>

using namespace std;

int main(void) {
	int n, S[5000] = { 0 }, numsMax = 0;
	while(1) {
		cin >> n;
		if(n == 0) { break; }
		for(int i = 0; i < n; i++) {
			cin >> S[i];
		}
		numsMax = 0;
		for(int i = 0; i < n; i++) {
			int preSum = 0;
			for(int j = i; j < n; j++) {
				preSum += S[j];
				numsMax = max(preSum, numsMax);
			}
		}
		cout << numsMax << endl;
	}
}

C++: #include <cstdio>
#include <algorithm>
#define INF 1e+18
#define MAX_SIZE 1 << 17
using namespace std;

typedef long long ll;

int n,k;
ll v[100000];
ll segMax[2][2 * MAX_SIZE - 1],segAdd[2][2 * MAX_SIZE - 1];

void add(int num,int a,int b,ll x,int k = 0,int l = 0,int r = n){
	if(b <= l || r <= a) return;
	if(a <= l && r <= b){
		segAdd[num][k] += x;
		return;
	}
	add(num,a,b,x,k * 2 + 1,l,(l + r) / 2);
	add(num,a,b,x,k * 2 + 2,(l + r) / 2,r);
	segMax[num][k] = max(segMax[num][k * 2 + 1] + segAdd[num][k * 2 + 1],segMax[num][k * 2 + 2] + segAdd[num][k * 2 + 2]);
}

ll get(int num,int a,int b,int k = 0,int l = 0,int r = n){
	if(b <= l || r <= a) return -INF;
	if(a <= l && r <= b) return segAdd[num][k] + segMax[num][k];
	return max(get(num,a,b,k * 2 + 1,l,(l + r) / 2),get(num,a,b,k * 2 + 2,(l + r) / 2,r)) + segAdd[num][k];
}

void init(){
	int t = 1;
	while(t < n + 1) t *= 2;
	for(int i = 0;i < t * 2 - 1;i++){
		segMax[0][i] = -INF;
		segMax[1][i] = -INF;
	}
	segMax[0][t - 1] = 0;
	for(int i = 0;i < n;i++) segMax[0][i + t] = segMax[0][i + t - 1] + v[i];
	ll ma = -INF;
	for(int i = n;i >= 0;i--) {
		ma = max(ma,segMax[0][i + t - 1]);
		segMax[1][i + t - 1] = ma - segMax[0][i + t - 1];
	}
	for(int i = t - 2;i >= 0;i--) {
		segMax[0][i] = max(segMax[0][i * 2 + 1],segMax[0][i * 2 + 2]);
		segMax[1][i] = max(segMax[1][i * 2 + 1],segMax[1][i * 2 + 2]);
	}
	n = t;
}

int main(){
	scanf("%d %d",&n,&k);
	for(int i = 0;i < n;i++) scanf("%lld",&v[i]);
	init();
	for(int i = 0;i < k - 1;i++){
		
	}
	printf("%lld\n",get(1,0,n));
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (((ll)1<<31)-1)
#define eps LDBL_EPSILON
#define moder (1000000007)
#define pie 3.141592653589793238462643383279
#define P std::pair<int,int>
#define prique priority_queue
#define ggr getchar();getchar();return 0;
using namespace std;
int n,a[5010],b[5010];
signed main(){
	while(true){
		cin>>n;
		if(!n)break;
		rep(i,n){
			cin>>a[i];
			if(i)a[i]+=a[i-1];
		}
		rep(i,n){
			if(i)b[i]=min(b[i-1],a[i]);
			else b[i]=min((ll)0,a[i]);
		}
		int ans=-INF;
		rep(i,n){
			ans=max(ans,a[i]-b[i-1]);
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;
    while(cin>>n,n){
        vi v(n+1,0);
        rep(i,n)cin>>v[i];
        ll ans=v[0], temp;
        for(int i=0;i<n;i++){
            temp=v[i];
            for(int j=i+1;j<n;j++){
                temp+=v[j];
                ans=max(ans,temp);
            }
        }
        cout<<ans<<endl;
    }
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<cctype>
#include<iomanip>
#include<functional>
#include<numeric>
#define EPS 1e-10
#define INF (-(1<<21))
using namespace std;
typedef long long llong;
int main() {
	int n;
	while (1) {
		cin >> n;
		if (!n)break;
		vector<vector<int> >v(n, vector<int>(n));
		vector<int>a(n);
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j)v[i][j] = a[i];
				else v[i][j] = INF;
			}
		}
		int max = INF;
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				if (i == j) {
					//cout << v[i][j] << " ";
					continue;
				}
				v[i][j] = v[i][j - 1] + a[j];
				if (max < v[i][j])max = v[i][j];
				//cout << v[i][j] << " ";
			}
			//cout << endl;
		}
		cout << max << endl;
	}
	return 0;
}