Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var infty = 100000 * 2
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() (int, error) {
	scanner.Scan()
	return strconv.Atoi(scanner.Text())
}

func findMaximumSubarray(arr []int) int {
	currentMaxSum, maxSumContainsTail := -infty, -infty

	for j := range arr {
		if arr[j] > maxSumContainsTail+arr[j] {
			maxSumContainsTail = arr[j]
		} else {
			maxSumContainsTail += arr[j]
		}
		if currentMaxSum < maxSumContainsTail {
			currentMaxSum = maxSumContainsTail
		}

		// fmt.Println(arr[j], currentMaxSum, maxSumContainsTail)
	}
	return currentMaxSum
}

func main() {
	scanner.Split(bufio.ScanWords)
	for {
		size, _ := nextInt()
		if size == 0 {
			break
		}

		arr := make([]int, size)
		for i := range arr {
			n, _ := nextInt()
			arr[i] = n
		}

		maximumSum := findMaximumSubarray(arr)
		fmt.Println(maximumSum)
	}
}

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

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <climits>
#include <utility>

#define PRINT(STR) cout << STR << endl;
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) (V).begin(), (V).end()

typedef long long ll;

using namespace std;

int nextInt() { int n = -1; cin >> n; return n; }
double nextDouble() { double d = -1; cin >> d; return d; }
ll nextll() { ll n = -1; cin >> n; return n; }
string nextString() { string str; cin >> str; return str; }

int INF = 1LL << 25;
double EPS = 1e-9;

// BEGIN //////////////////////////////////////////////////////////////

int main() {
	while (true) {
		int N = nextInt();
		if (N == 0) break;
		int score_max = -INF, score = 0;
		REP(i, N) {
			int d = nextInt();
			score += d;
			score_max = max(score, score_max);
			score = max(score, 0);
		}
		PRINT(score_max);
	}

	return 0;
}

// END   //////////////////////////////////////////////////////////////

C++: #include <stdio.h>
int arr[] = int[5000];
int sub[] = int[5001];
int main() {
  while(1) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      return 0;
    }
    for(int i = 0; i < n; ++i) {
      scanf("%d", &arr[i]);
    }
    sub[0] = 0;
    for(int i = 1; i < n+1; ++i) {
      sub[i] = sub[i-1] + arr[i-1];
    }
    for(int i = 0; i < n; ++i) { 
        for(int j = i + 1; j < n + 1; ++j) {
          m = max(m, sub[j] - arr[i]);
        }
    }
    printf("%d", m);
  }
}

C++: #include<stdio.h>

int main(){
	long int n,a[5000],b[5000],i,j,max;
	
	while(0<=scanf("%ld",&n)){
		if(n==0)break;
		max=-2147483647;
		for(i=0;i<n;i++){
			scanf("%ld",&a[i]);
			if(max<a[i])max=a[i];
		}
		b[0]=a[0];
		for(i=1;i<n;i++){
			b[i]=b[i-1]+a[i];
			if(b[i]>max)max=b[i];
		}
		
		for(i=1;i<n;i++){
			for(j=i;j<n;j++){
				if(max<b[j]-b[i-1])max=b[j]-b[i-1];
			}
		}
		
		printf("%ld\n",max);
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main(){
	int n;
	int A[5000];
	int sum=0,max=0;
	scanf("%d",&n);
	while(n != 0){
			memset(A,0,sizeof(A));
			for(int i=0;i<n;i++)cin >> A[i];

			for(int i=0;i<n;i++){
				int next = i + 1;
				sum = A[i];
				if(sum > max)max = sum;
				while(next < n){
					sum += A[next];
					if(sum > max)max = sum;

					next++;
				}
			}

			cout << max << endl;
			max = 0;
			scanf("%d",&n);	
	}


}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <cctype>
#include <cstdlib>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a,b) (fabs((a)-(b)) < EPS)

int main(){
	int n;
	while (cin >> n && n != 0) {
		int ret = -INF;
		int a[5000];
		for (int i = 0; i < n; ++i) cin >> a[i];
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				int sum = 0;
				for (int k = i; k <= j; ++k) sum += a[k];
				ret = max(ret, sum);
			}
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(){
	int n,max=0;
	while(cin>>n&&n!=0){
		int a[5000]={0};
		for(int x=0;x<n;x++){
			cin>>a[x];
			for(int i=1;i<=n;i++){
				for(int j=0;j<n;j++){
					int sum=0;
					for(int k=j;k<i;k++){
						sum+=a[k];
					}
					if(max<sum)max=sum;
				}
			}
		}
		cout<<max<<endl;
			max=0;
	}
	return 0;
}