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 <vector>
using namespace std;

int maxsum(const vector<int>& data){
  int maxsumval=0,n=data.size();
  for(int i=0;i<n-1;i++){
    for(int j=0;j<n-i;j++){
      int sum=0;
      for(int k=0;k<n-i;k++){
        sum+=data[i+k];
      }
      if(sum>maxsumval)maxsumval=sum;
    }
  }
  return maxsumval;

}
int main(){
  int n;
  while(cin>>n&&n){
    vector<int> data(n);
    for(int i=0;i<n;i++){
      cin>>data[i];
    }
    int maxsumval=maxsum(data);
    cout<<maxsumval<<endl;
  }

}

C++: #include <iostream>

using namespace std;

int main()
{
	int n;

	while (1)
	{
		int a[5001] = {0}, s[5001] = {0}, max = 0;

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

		for (int i=1; i<=n; i++)	//入力
			cin >> a[i];

		for (int i=1; i<=n; i++) {
			for (int j=1; j<=i; j++) {
				s[i] += a[j];		//s[i] = a[1]+a[2]+...+a[i]
			}
		}

		for (int i=1; i<=n; i++) {
			for (int j=n; j>=i; j--) {
				max = (max < s[j] - s[i-1]) ? s[j] - s[i-1] : max;	//区間i,jの和を求めて比較
			}
		}

		cout << max << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define REP(i,n) for(int i = 0; i < (n); ++i)
using namespace std;

// ?¨±?????????????????
#define EPS (1e-10)

int main(){
	int n;
	while(cin >> n && n != 0){
        vector<int> a(n);
        REP(i, n) cin >> a[i];
        vector<int> sum(n);
        sum[0] = a[0];
        FOR(i, 1, n) sum[i] = sum[i-1] + a[i];
        int ret = sum[0];
        FOR(i,-1, n) FOR(j,i+1, n) ret = i > -1 ? max(ret ,sum[j]-sum[i]) : max(ret ,sum[j]) ; 
        cout << ret << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <complex>
#include <functional>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(c,i) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		vi a(n);
		rep(i,n)
			cin>>a[i];
		
		vector<ll> sum(1);
		rep(i,n)
			sum.push_back(sum.back()+a[i]);
		ll res=0;
		rep(i,n)
			repi(j,i+1,n+1)
				res=max(res,sum[j]-sum[i]);
		cout<<res<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)


using namespace std;

int main (void){
  
    int number;
    int max;
    int answer[500] = {0};
    int l = 0;
    while(cin >> number,number)
    {
        int a[5010] = {0};
        vector<int>tempmax(500.500);
        vector<int>tempmax2(1000);
        REP(i,number)
        cin >> a[i];
        for(int i = number;i>=0;i--)
            for(int j = 0; j<i;j++)
            {
                tempmax[i] +=a[j];
            }
        for(int k = 0;k<number;k++)
            for(int m = k;m<number;m++)
            {
                if(k == m)
                    tempmax2[k] = a[k];
                else if(tempmax2[k]+a[m] >= tempmax2[k])
                    tempmax2[k]+= a[m];
                else
                    break;
            }
        sort(tempmax.begin(),tempmax.end(),greater<int>());
        sort(tempmax2.begin(),tempmax2.end(),greater<int>());
        if(tempmax[0]>tempmax2[0])
            answer[l] = tempmax[0];
        else
            answer[l] = tempmax2[0];
        l++;
    }
    REP(i,l)
    cout << answer[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
    while(1){
        int n;
        cin>>n;
        if(n==0){
            return 0;
        }
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a.at(i);
        }
        vector<int> b(n+1);
        b.at(0)=0;
        for(int i=1;i<=n;i++){
            b.at(i)=b.at(i-1)+a.at(i-1);
        }
        int ans=b.at(1);
        for(int i=1;i<=n;i++){
            for(int j=0;j<i;j++){
                ans=max(ans,b.at(i)-b.at(j));
            }
        }
        cout<<ans<<endl;
    }
}

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];
						}
					}
				}
			}
			if(max < tmpmax2){
				max = tmpmax2;
			}
		}
		delete [] factor;
		cout << max << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;





int main()
{
	int n;
	long long a[5000];
	long long b[5000];//i???????????§??????
	long long m, M;
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n; i++) {
			if (i == 0) b[i] = a[i];
			else b[i] = b[i - 1] + a[i];
		}
		for (int i = 0; i < n; i++) {
			if (i == 0) {
				m = min({ (long long)0,b[0] });
				M = max({ (long long)0,b[0] });
			}
			else {
				if (b[i] - m > M) {
					M = b[i] - m;
				}
				if (b[i] < m) {
					m = b[i];
				}
			}
		}
		if (M == 0 && b[0] < 0) {
			for (int i = 0; i < n; i++) {
				if (i == 0) M = b[i];
				else {
					if (b[i] > M) M = b[i];
				}
			}
		}
		cout << M << endl;
	}
		





}

C++: #include<iostream>
using namespace std;
int main() {
	int n;
	int a[5000];
	while (cin >> n&&n != 0) {
		int sum3 = 0;
		for (int i = 0; i<n; i++)
			cin >> a[i];
		for (int i = 0; i<n; i++) {
			int sum = 0;
			int sum2 = 0;
			for (int j = 0; j <= i; j++) {
				if (a[j]<0||(j<=1&&a[j-1]<0&&(a[j-1]+a[j])<0)) goto stop;;
				sum2 = 0;
				for (int z = j; z <= i; z++) {
					sum2 += a[z];
					if (sum<sum2) sum = sum2;
				}
			stop:;
			}
			if (sum3<sum) sum3 = sum;
		}
		cout << sum3 << endl;
	}
}