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 <bits/stdc++.h>
using namespace std;

#define DEBUG(x) cerr << #x << ": " << x << endl;
#define ll long long

int main() {
    vector<int> ans;
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;

        vector<int> v(n);
        for (int &x : v) cin >>x;

        for (int i = 1; i < n; i++) {
            v[i] = v[i] + v[i-1];
        }

        int Max = v[0];    
        int a = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                Max = max(Max, v[j] - a);
            }
            a = v[i];
        }

        ans.push_back(Max);
    }

    for (int x : ans) {
        cout << x << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <map>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;

#define REP(i,n) for(int i=0; i<n; i++)

int n;
vector<int> v;
map<int,long long> s[5000];

//long long s[5000][5000];

int main(){
    while(cin>>n,n){
        v=vector<int>(n);
        REP(i,n)cin>>v[i];
        long long ans=-(1<<30);
        REP(i,n){
            s[i][i]=v[i];
            ans=max(s[i][i],ans);
            for(int j=i+1; j<n; j++){
                s[i][j]=s[i][j-1]+v[j];
                ans=max(s[i][j],ans);
            }
        }
        cout<<ans<<endl;
    }
}

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 sum[5001];
int main(){
	int N;
	int ans = 0;
	while(cin>>N&&N){
		int index=0;
		ans = INT_MIN;
		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-1]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: //#define _USE_MATH_DEFINES
//#include <cmath>
#include <iostream>
//#include <stdio.h>
//#include <stdlib.h>
//#include <iomanip>
#include <vector>
//#include <string>
//#include <algorithm>
//#include <functional>
//#include <time.h>
//#include <sstream>
//#include <queue>

using namespace std;


int main(){

	int n, buf,min,max;
	long long ans, sum;
	vector <int> p;


	while (1){
		p.clear();
		sum = 0;
		ans = -100000;

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

		for (int i = 0; i < n; i++){
			cin >> buf;
			p.push_back(buf);
		}

		for (int left = 0; left < n; left++){
			if (p[left] >= 0){
				for (int right = left; right < n; right++){
					if (p[right] >= 0){
						for (int i = left; i <= right; i++){
							sum += p[i];
						}
						if (sum > ans) ans = sum;
						sum = 0;
					}
				}
			}
		}
		if (ans == -100000){
			for (int i = 0; i < n; i++){
				if (p[i]>ans)ans = p[i];
			}
		}


		cout << ans << endl;

	}

	return 0;
}

C++: //まともに使い方を理解していないくせにincludeしまくる人間のクズ
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <iostream>


//using namespace stdしないと表記がめんどくさいからね、仕方ないね
using namespace std;

//可読性を上げるためのdefineが仇となる典型的パターン
#define rep(i,n) for(int i = 0; i < x; i++)

//ローカル変数とグローバル変数は使い分けよう！
stack<short> x, y;
long long int h, m, s, ans, n, array[5000];

//関数って楽しいよね！（魔王スマイル）

void solve(long long int sum, short i){
    if(ans < sum) ans = sum;
    if(i = n) return;
    solve(sum + array[i], i + 1);
    solve(0, i + 1);
}

int main(){
    while(true){
        scanf("%lld", &n);
        if(n == 0) return 0;
        ans = 0;
        rep(i,n) scanf("%lld", &array[i]);
        solve(0, 0);
        printf("%d\n", ans);
    }
}

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

int main()
{

	int n, num;
	vector< int > a;

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

		int Max = -100001;

		a.push_back( 0 );

		for( int i = 0; i < n; i++ ) {

			cin >> num;
			a.push_back( num );

		}

		for( int i = 1; i < n; i++ ) {

			a[i] = max( a[i], a[i] + a[i - 1] );
			Max  = max( Max, a[i] );
		
		}

		cout << Max << endl;

		a.clear();
	
	}

	return 0;

}

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


int main(){
  int a[50000];
  int d; // d[i] : a[i]ツ暗按降ツづ勲SS ツづ個催妥・ツ値
  int n;

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

    int mx = a[0];
    for( int i=0;i<n;i++ ){
      int sum =a[i];
      d=sum;
      for( int j=i+1;j<n;j++ ){
	sum+=a[j];
	d = max( d,sum );
      }
      mx = max( mx,d );
    }
    cout << mx << endl;
  }

  return 0;
}

C++: #include <iostream>
using namespace std;
int main (){
while (1){
long n,dset[5000],temp=0,MINEdset[5000],max=-1100000,h[5000],b=0;
cin>>n;
if (n==0)break;
for (int i=0;i<n;i++){
        cin>>dset[i];
}
for (int i=0;i<n;i++){
        for (int j=i;j<n;j++){
                temp=temp+dset[j];
                MINEdset[j]=temp;
        }
        for (int j=i;j<n;j++){
                if (max<MINEdset[j]){
                        max=MINEdset[j];
                }
        }
        h[b]=max;
        max=-1100000;
        b++;
        temp=0;
}
max=-1100000;
for (int i=0;i<=b;i++){
        if (max<h[i]){
                max=h[i];
        }
}
cout<<max<<endl;
}
return 0;
}

C++: #include <iostream>

int maxSum(int *a, int n) {
	int sum, max = a[0];
	for (int i = 0; i < n; ++i) {
		sum = 0;
		for (int j = i; j < n; ++j) {
			sum += a[j];
			if (sum > max) { max = sum; }
		}
	}
	return max;
}

int main() {
	int n;
	int *a;
	for (;;) {
		std::cin >> n;
		if (n <= 0) { break; }
		a = new int[n];
		for (int i = 0; i < n; ++i) {
			std::cin >> a[i];
		}
		std::cout << maxSum(a, n) << '\n';
		delete [] a;
	}
	return 0;
}

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

int main(){
    int n,a[MAX];
    
    while(cin >> n,n){
        for(int i = 0 ; i < n ; i++) cin >> a[i];
        
        int max = a[0];
        for(int i = 0 ; i < n ; i++){
            int tmp = 0;
            for(int j = i ; j < n ; j++){
                tmp += a[j];
                if(max < tmp) max = tmp;
            }
        }
        cout << max << endl;
    }
    
    return 0;
}

C++: #include<cstdio>
#include<vector>

using namespace std;

int main(void){
	int n;
	while(scanf("%d", &n) && n){
		
		int num[5001]={0};
		int total[5001]={0};
		int ans = -2000000000;
		
		for(int i = 0; i < n; i++){
			scanf("%d", &num[i]);
		}
		
		for(int i = 0; i < n; i++){
			for(int j = i; j < n; j++){
				total[i] += num[j];
				ans = max(ans,total[i]);
			}
		}
		printf("ans ===   %d\n",ans);
	}
	return 0;
}