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 <vector>

using namespace std;

int findMax(vector<int>& a) {
  int sum, max = 0;
  for (int i = 0; i < a.size(); i++) {
    for (int j = i, sum = 0; j < a.size(); j++) {
      sum += a[j];
      if (max < sum) max = sum;
    }
  }
  return max;
}

int main() {
  int n, t, max;
  while (1) {
    cin >> n;
    if (n == 0) break;
    vector<int> a;
    for (int i = 0; i < n; i++) {
      cin >> t;
      if (-100000 <= t && 100000 >= t)
        a.push_back(t);
    }
    cout << findMax(a) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;

#define ull unsigned long long
#define ll long long
#define rep(i,j) for(int i=0;i<j;++i)
#define loop(i,a,j) for(int i=a;i<j;++i)
#define mpi(it,table,T,E) for(map<T,E>::iterator it = table.begin();it!=table.end();++it)
#define PI 3.141592654
/*
ifstream fin("input.txt");
ofstream fout("output.txt");
#define cin fin
#define cout fout
*/
#define N 5005

int sol[N][N];
bool visited[N][N];
int data[N];

int solve(int i,int j,int sum){
	if( i == j) return sum;
	if(visited[i][j])return sol[i][j];
	
	int a = max(sum,solve(i+1,j,sum-data[i]));
	int b = max(sum,solve(i,j-1,sum-data[j]));
	sol[i][j] = max(a,b);

	visited[i][j] = true;
	return sol[i][j];
}


int main(){
	int n;
	while(scanf("%d",&n),n){
		memset(visited,0,sizeof(visited));
		memset(sol,0,sizeof(visited));
		int sum = 0;
		rep(i,n){
			scanf("%d",&data[i]);
			sum += data[i];
		}
		sum = solve(0,n-1,sum);
		printf("%d\n",sum);	
	}
	return 0;
}

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 main(){
	int N;
	int x;
	while(1){cin>>N;if(N==0) break;int index=0;
		ll ans = INT_MIN;
		vl sum(N+1);
		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-2]);
			}
		}
		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 x;
	while(cin>>N&&N){
		int index=0;
		int ans = 0;
		
		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-2]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: #include <iostream>
#include <iomanip>
#include <math.h>
#include <utility>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <climits>
using namespace std;

int main(){
	int n;
	int *a,*S;
	while(cin >> n){
		if(n==0) break;
		a = new int[n];
		S = new int[n+1];
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		
		int sum = 0;
		for(int i=0;i<n+1;i++){
			S[i]=sum;
			sum += a[i];
		}
		int ans=INT_MIN;
		for(int i=0;i<n+1;i++){
			for(int j=i+1;j<n+1;j++){
				ans = max(ans,S[j]-S[i]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,i,j;
  cin >> n;
  while(n!=0) {
    long long sum[5001]={};
    int b;
    long long o=1<<20;
    o*=-1;
    cin >> sum[1];
    for(i=2;i<=n;i++){
      cin >> b;
      sum[i]=sum[i-1]+b;
      //cout << sum[i] << endl;
    }
    
    for(i=0;i<=n;i++){
      for(j=i+1;j<=n;j++){
	o=max(o,sum[j]-sum[i]);
      }
    }
    cout << o << endl;
    cin >> n;
  }
  return 0;
  
}

C++: /* AOJ 0022 Maximum Sum Sequence */

#include <iostream>
#include <climits>

using namespace std;

void update(int i, int x);
void init();

const int MAX_N = 5000;

int max_sum[MAX_N + 1];
int diff_from_max[MAX_N + 1];
int sum_last_non_negative[MAX_N + 1];

int
main()
{
    for (;;) {
        int n, i;

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

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

            cin >> a;
            update(i, a);
        }

        cout << max_sum[n] << endl;
    }

    return 0;
}

void
update(int i, int x)
{
    if (x < 0) {
        if (max_sum[i - 1] <= x) {
            max_sum[i] = x;
            diff_from_max[i] = x;
        } else {
            max_sum[i] = max_sum[i - 1];
            diff_from_max[i] = diff_from_max[i - 1] + x;
        }

        sum_last_non_negative[i] = -1;
    } else {
        int diff_from_max_temp = diff_from_max[i - 1] + x;

        if (sum_last_non_negative[i - 1] < 0)
            sum_last_non_negative[i] = x;
        else
            sum_last_non_negative[i] = sum_last_non_negative[i - 1] + x;

        if (diff_from_max_temp > 0) {
            // 最大値を更新する
            int cumulative_sum = max_sum[i - 1] + diff_from_max_temp;

            max_sum[i] = max(sum_last_non_negative[i], cumulative_sum);
            diff_from_max[i] = 0;
        } else {
            if (max_sum[i - 1] < sum_last_non_negative[i]) {
                // 最大値を更新する
                max_sum[i] = sum_last_non_negative[i];
                diff_from_max[i] = 0;
            } else {
                max_sum[i] = max_sum[i - 1];
                diff_from_max[i] = diff_from_max_temp;
            }
        }
    }
}

void
init()
{
    max_sum[0] = INT_MIN;
    sum_last_non_negative[0] = -1;
}