Go: package main

import (
        "fmt"
)

func main() {
        for {
                var n int
                _, err := fmt.Scanf("%d", &n)
                if err != nil || n == 0 {
                        break
                }

                var sum [5001]int

                for i := 1; i <= n; i++ {
                        fmt.Scanf("%d", &(sum[i]))
                        if i > 1 {
                                sum[i] += sum[i-1]
                        }
                }

                max_sum := sum[1]
                for i := 1; i <= n; i++ {
                        for j := 0; j < i; j++ {
                                if max_sum < sum[i] - sum[j] {
                                        max_sum = sum[i] - sum[j]
                                }
                        }
                }
                fmt.Printf("%d\n", max_sum)
        }
}

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;
}

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#endif

#define ITER(c)         __typeof__((c).begin())
#define FOREACH(it, c)  for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)        __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)       REPEAT(i, 0, n)
#define REPEAT(i, k, n) for (int i = (k); i < (n); ++i)
#define REVREP(i, n)    for (int i = ((n)-1); i >= 0; --i)
#define FIND(c, n)      std::find((c).begin(), (c).end(), (n));
#define BSEARCH(c, n)   std::binary_search((c).begin(), (c).end(), (n))
#define LLPOW(p1,p2)    ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)      (sizeof(A)/sizeof((A)[0]))
#define MIN(p1,p2)      (((p1)<(p2))?(p1):(p2))
#define MAX(p1,p2)      (((p1)>(p2))?(p1):(p2))
#define CIN_NO_SYNC     do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)       fgets((p), sizeof(p), stdin)
#define CHOMP(p)        do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n') *_q = 0; } while(0)

int main(){
    int n;
    while(cin >> n, n){
        list<int> v;
        int p = 0;
        REP(i, n){
            int in;
            cin >> in;
            if((p <= 0 && in < 0) || p >= 0 && in > 0 || in == 0){
                p += in;
            } else {
                v.push_back(p);
                p = in;
            }
        }
        v.push_back(p);

        while(v.size() >= 3){
            //FOREACH(it, v){ cout << *it << ", ";} cout << endl;
            bool f = false;
            FOREACH(it, v){
                int p1 = *it;
                if(p1 < 0) continue;
                ITER(v) it2 = it; it2++;
                if(it2 == v.end()) break;
                int p2 = *it2;
                ITER(v) it3 = it2; it3++;
                if(it3 == v.end()) break;
                int p3 = *it3;

                int sum = p1 + p2 + p3;
                if(sum > p1 && sum > p3){
                    v.erase(it2);
                    v.erase(it3);
                    *it += (p2+p3);
                    f = true;
                    break;
                }
            }
            if(!f) break;
        }

        int max = INT_MIN;
        FOREACH(it, v){
            if(max < *it) max = *it;
        }
        cout << max << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	long long int n,a[5000],saidai=-100000;
	while(cin>>n&&n!=0){
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		for(int i=0;n>i;i++){
			for(int x=n;x>=i;x--){
				int long long kazu=0;
				for(int y=x;y>=i;y--){
					kazu=kazu+a[y];
				}
				if(kazu>saidai){
					saidai=kazu;
				}
			}
		}
		cout<<saidai<<endl;
	}
}

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


int main()
{
    int sum;
    int currentSum;
	int loop;
	int a[5005];

	while (cin >> loop, loop) {
	    // O(n)
	    for(int i=0; i<loop; i++){
	        cin >> a[i];
	    }
	    
	    // O(n^2)
	    sum=0, currentSum=0;
	    for(int j=0; j<loop; j++){
	        for(int k=j; k<loop; k++){
	            //cout << "k : " << k << endl;
	            currentSum += a[k];
	            if(currentSum > sum){
	                sum = currentSum;
	            }
	        }
	        currentSum = 0;
	    }
        cout << sum << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<complex>
#include<cfloat>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define EPS (1e-10)
using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;

#define PI (3.1415926535)
#define EPS (1e-10)

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

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

int main(){
	int n;
	int a;
	int sum;
	int sum2;
	int max;
	cin >> n;
	while(n){
		cin >> sum;
		max = 0;
		for(int i=0;i<n-1;i++){
			cin >> sum2;
			sum += sum2;
			if(sum < sum2){
				sum = sum2;
			}
			if(sum > max){
				max = sum;
			}
		}
		cout << max << endl;
		cin >> n;
	}
	return 0;
}

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;
	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++){
			for (int right = left; right < n; right++){
				for (int i = left; i <= right; i++){
					sum += p[i];
				}
				if (sum > ans) ans = sum;
				sum = 0;
			}
		}

		cout << ans << endl;

	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif

int main(int argc, char **argv){
	int n = 0;
	std::vector<int> a;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
#endif

	for(;;){
#if INPUT_FROM_FILE
		ifs >> n;
#else
		std::cin >> n;
#endif
		if(n == 0){ break; }

		a.resize(n);
		for(int i = 0; i < n; i++){
#if INPUT_FROM_FILE
			ifs >> a[i];
#else
			std::cin >> a[i];
#endif
		}

		long max_sum = -1000000000;

		for(int i = 0; i < n; i++){
			long sum = a[i];
			if(max_sum < sum){
				max_sum = sum;
			}
			for(int j = i + 1; j < n; j++){
				sum += a[j];
				if(max_sum < sum){
					max_sum = sum;
				}
			}
		}

		std::cout << max_sum << std::endl;
	}

	return 0;
}