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 <cassert>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>

int solve(std::istream& in, int n)
{
    std::vector<int> all(n);
    std::vector<int> koho;
    int temp = 0;
    int max = 0;
    for(int i = 0; i < n; ++i) {
        in >> all[i];
        if(temp && all[i] < 0){
            koho.push_back(temp);
            if(temp + all[i] < 0){
                temp = 0;
            }else{
                temp += all[i];
            }
        }else
        if(all[i] > 0){
            temp += all[i];
        }
    }
    koho.push_back(temp);
    if(koho.empty()) return *std::max_element(all.begin(), all.end());
    return *std::max_element(koho.begin(), koho.end());
}
int main()
{
    int n;
    std::cin >> n;
    while(n){
        std::cout << solve(std::cin, n) << std::endl;
        std::cin >> n;
    }
    return 0;
}

C++: #include <iostream>

#include <vector>

#include <algorithm>

#include <sstream>

#include <string>

#include <cmath>

#include <list>

#include <set>
#include <cstdlib>
using namespace std;

int in() {int x; cin >> x; return x;}

template <typename T> class SegTree

{

public:

	int n;

	T T_max;

	vector<T> dat;

	SegTree(int n_, T t_max)

	{

		int n = 1;

		T_max = t_max;

		while (n < n_)n *= 2;

		for (int i = 0; i < 2 * n - 1; i++)dat.push_back(t_max);

	}

	void update(int k, T a)

	{

		k += n - 1;

		dat[k] = a;

		while (k > 0)

		{

			k = (k - 1) / 2;

			dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);

		}

	}



	//[a,b)

	T query(int a, int b)

	{

		return _query(a, b, 0, 0, n);

	}



private:

	T _query(int a, int b, int k, int l, int r)

	{

		if (r <= a || b <= l)return T_max;

		if (a <= l && r <= b)return dat[k];

		else

		{

			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);

			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);

			return min(vr, vl);

		}

	}

};


int main()
{
	int n;
	while (cin >> n && n)
	{
		vector<int> a;
		int ans = -999999999;
		for (int i = 0; i < n; i++)
		{
			a.push_back(in());
		}
		for (int i = 1; i < n; i++)
		{
			a[i] += a[i - 1];
		}

		for (int l = 0; l < n; l++)
		{
			for (int r = l; r < n; r++)
			{
				ans = max(ans, a[r] - a[l]);
			}
		}
		cout << ans << endl;


	}
}

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){
				++c;
				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<cstring>
#define N 5001
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main()
{
    int n;
    while(cin >> n &&n)
    {
        if(n==0)break;
        long int s[N];
        //memset(s,0,sizeof(int)*N);
        int t;
        long int max = -N*100000;
        long int min = 0;
        for(int i=0;i<n;i++){
            cin >>t;
            s[i+1] = s[i]+t;
            if(max<s[i+1])max = s[i+1];
            if(min>s[i+1])min = s[i+1];
        }
        cout << max -min << endl;

    }

    return 0;
}

C++: using System;
using System.Collections.Generic;
using System.Linq;

class Program {

    static int Main(string[] args) {
        List<int> inList = new List<int>();

        while (true) {
            string v = Console.ReadLine();
            if (v == "0") { break; }

            inList.Add(int.Parse(v));
        }

        for (int x = 0; x < inList.Count; x++) {
            int n = inList[x];

            List<int> list = new List<int>();
            for (int i = 0; i < n; i++) {
                x++;
                list.Add(inList[x]);
            }

            int maxValue = 0;
            for (int i = 0; i < list.Count; i++) {
                int value = list[i];
                if (maxValue < value) { maxValue = value; }

                for (int k = i + 1; k < list.Count; k++) {
                    value += list[k];
                    if (maxValue < value) { maxValue = value; }
                }
            }

            Console.WriteLine(maxValue);
        }

        return 0;
    }
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;

int main(){
	int num[5000],sum[5001],n,i;
	while(1){
		sum[0] = 0;
		scanf("%d",&n);
		if(!n) break;
		for(i = 0;i < n;i++){
			scanf("%d",&num[i]);
			sum[i + 1] = sum[i] + num[i];
		}
		sort(sum,sum + n + 1);
		printf("%d\n",sum[n] - sum[0]);
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF LLONG_MAX
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;

#define ARRAY_MAX 5005

int main(){


    ll n;
    while(cin >> n,n){
        vector<ll> arr(ARRAY_MAX,0);
        REP(i,n){
            cin >> arr[i+1];
        }
        arr[0] = 0;
        REP(i,n){
            arr[i+1] += arr[i];
        }

        ll maxi = -INF;
        for(int i = n;i >= 0;i--){
            for(int j = 0;j < i-1;j++){
                maxi = max(maxi,arr[i]-arr[j]);
            }
        }
        cout << maxi << endl;
    }

    return 0;
}

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

using namespace std;

int intervalSum(int cs[], int a, int b);

int main(void) {
	while(1) {
		int n, ans = -10000;
		int array[5000], cumsum[5000] = { 0 };
		
		cin >> n;
		if(n == 0) { break; }
		
		for(int i = 0; i < n; i++) {
			cin >> array[i];
			if(i == 0) { cumsum[i] = array[i]; }
			else { cumsum[i] = array[i] + cumsum[i-1]; }
		}
		
		for(int i = 0; i < n; i++) {
			for(int j = i; j < n; j++) {
				ans = max(ans, intervalSum(cumsum, i, j));
			}
		}
		
		cout << ans << endl;
	}
	
	return 0;
}

int intervalSum(int cs[], int a, int b) {
	return cs[b] - cs[a-1];
}