Go: package main

import "fmt"

func main() {
	var result []int
	for {
		var n, a, maxSum int
		fmt.Scan(&n)

		if n == 0 {
			break
		}

		maxSum = 0
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			if i == 0 {
				sum = a
				maxSum = sum
			} else if sum+a < a {
				sum = a
				if maxSum < sum {
					maxSum = sum
				}
			} else {
				sum = sum + a
				if maxSum < sum {
					maxSum = sum
				}
			}
		}

		result = append(result, maxSum)
	}

	for _, v := range result {
		fmt.Println(v)
	}
}

C++: #include <iostream>

#include <limits>
#include <tuple>
#include <vector>

// left: [low, mid)  right: [mid, high)
template<typename T>
std::tuple<std::size_t, std::size_t, T> find_max_crossing_subarray
(const std::vector<T> &A, std::size_t low, std::size_t mid, std::size_t high) {
    std::size_t max_left = mid - 1;
    std::size_t max_right = mid;

    T left_sum = std::numeric_limits<T>::min();
    T sum = 0;
    for(std::size_t i = 1; i <= mid - low; ++i) {
        sum += A[mid - i];
        if(sum > left_sum) {
            left_sum = sum;
            max_left = mid - i;
        }
    }

    T right_sum = std::numeric_limits<T>::min();
    sum = 0;
    for(std::size_t j = mid; j < high; ++j) {
        sum += A[j];
        if(sum > right_sum) {
            right_sum = sum;
            max_right = j;
        }
    }

    return std::make_tuple(max_left, max_right + 1, left_sum + right_sum);
}

// [low, high)
template<typename T>
std::tuple<std::size_t, std::size_t, T> find_maximum_subarray
(const std::vector<T> &A, std::size_t low, std::size_t high) {
    if(high - low < 2) return std::make_tuple(low, high, A[low]);

    std::size_t mid = (low + high) / 2;
    auto left = find_maximum_subarray<T>(A, low, mid);
    auto right = find_maximum_subarray<T>(A, mid, high);
    auto cross = find_max_crossing_subarray<T>(A, low, mid, high);

    T left_sum = std::get<2>(left);
    T right_sum = std::get<2>(right);
    T cross_sum = std::get<2>(cross);

    if(left_sum >= right_sum && left_sum >= cross_sum) return left;
    else if(right_sum >= left_sum && right_sum >= cross_sum) return right;
    else return cross;
}

int main() {
    std::vector<int> A(5000);
    while(true) {
        std::size_t n;
        std::cin >> n;
        if(n == 0) break;

        for(std::size_t i = 0; i < n; ++i) std::cin >> A[i];
        std::cout << std::get<2>(find_maximum_subarray<int>(A, 0, n)) << std::endl;
    }
}

C++: #include <algorithm>
#include <iostream>
using namespace std;
int a[5010];
int main(){
	int i,n,max_so_far,max_ending_here;//maximum subarray problem
	cin>>n;
	while(n!=0){

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

		max_ending_here=0;
		max_so_far=0;
		
		for(i=0;i<n;i++){
			max_ending_here = max(0,max_ending_here + a[i]);
			max_so_far = max(max_so_far,max_ending_here);
		}
	cout << max_so_far << endl;
	cin>>n;//kousinn
	}
}

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

int main() {
	int i, j, n, a, max_so_far, max_ending_here;
	
	while (1) {
		cin >> n;
		if (n == 0)break;
		max_so_far = 0;
		max_ending_here = 0;
		for (i = 0; i < n; i++) {
			cin >> a;
			max_ending_here = max_ending_here + a;
			if (max_ending_here < 0)max_ending_here = 0;
			if (max_so_far < max_ending_here)max_so_far = max_ending_here;
		}
		cout << max_so_far << endl;

	}

	return 0;
}

C++: /*Filename: aoj0022.cpp
 *Author: Mushiyo
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int main(){
	int n = 0;

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

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

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

        int maxSum = INT_MIN;
        int minSum = INT_MAX;
        for(int i = 0; i < n; ++i){
        	maxSum = max(maxSum, sum[i] - minSum);
        	minSum = min(minSum, sum[i]);
        }
        
        cout << maxSum << endl;
    }

	return 0;
}

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