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 <vector>
#include <limits.h>
using namespace std;
int main()
{
	int n, k, p, m;
	bool isPlus = true;
	while (cin >> n){
		if (0 == n) break;
		vector<int> A(n, 0);
		m = INT_MIN;
		p = 0;
		for (int i = 0; i < n; ++i){
			cin >> k;
			if (m < k) m = k;
			if (0 == i){
				A[p] = k;
				isPlus = (k > 0);
			}
			else {
				if (isPlus == (k > 0)){
					A[p] += k;
				}
				else{
					++p;
					A[p] = k;
					isPlus = !isPlus;

					while (1 < p && isPlus){
						if (m < A[p - 2]) m = A[p - 2];
						if (m < A[p]) m = A[p];
						int a = A[p - 2] + A[p - 1] + A[p];
						if ((a > A[p - 2]) && (a > A[p])){
							A[p - 2] += A[p-1] + A[p];
							A[p - 1] = A[p] = 0;
							p -= 2;
						}
						else break;
					}
					if (m < A[p]) m = A[p];
				}
			}
		}
		if (m < A[p]) m = A[p];
		cout << m << "\n";
	}
	return 0;
}

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

// warning: include *e
int getBackMaxSum(int* s, int *e) {
	int sum = 0, maxSum = 0x80000000;
	for (; s <= e; e--) {
		sum += *e;
		maxSum = max(sum, maxSum);
	}
	return maxSum;
}

int main () {
	while (cin >> n, n) {
		int a[n], maxSum = 0x80000000;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			maxSum = max(maxSum, getBackMaxSum(a, a + i));
			assert(a[i] <= maxSum);
		}
		cout << maxSum << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> a(n, 0); //?´??????¨
    vector<int> b(n, 0); //????????¨
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int k = n - 1;
    while (a[k] < 0)
      k--;
    b[k] = a[k];
    for (int i = k - 1; i > -1; i--) {
      if (a[i] < 0)
	continue;
      else {
	int j = i + 1;
	int s = 0; //?????????????????????????????¨
	while (a[j] < 0) {
	  s += a[j];
	  j++;
	}
	//cout << "s = " << s << " " << "i = " << i << " " << "j = " << j << endl;
	if (s + b[j] >= 0)
	  b[i] += s + b[j] + a[i];
	else // if (a[i] > s + b[j])
	  b[i] += a[i];
	//cout << "b[" << i << "]= " << b[i] << endl;
      }
    }
    /*
    for (int i = 0; i < n; i++)
      cout << "b[" << i << "]= " << b[i] << endl;
    */
    int max = b[0];
    for (int i = 1; i < n; i++)
      if (max < b[i])
	max = b[i];
    cout << max << endl;
  }
}

C++: #include <iostream>

using namespace std;

int main(void){
	int n;
	long long int a[5000]={0};
	while(cin >> n){
		long long int max = 0;
		if(n == 0) break;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		for(int i=0;i<n;i++){
			long long int sum = 0;
			for(int j=0;i+j<n;j++){
				sum += a[i+j];
				if(max < sum) max = sum;
			}
		}
		cout << max << "\n";
	}
	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>


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

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

//ローカル変数とグローバル変数は使い分けよう！
stack<short> x, y;
unsigned long int ans, array[5000];
short i, n;
//関数って楽しいよね！（魔王スマイル）

void solve(unsigned 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;
        for(i = 0; i < n; i++) scanf("%lld", &array[i]);
        solve(0, 0);
        printf("%lld\n", ans);
    }
}

C++: #include<stdio.h>

int main(void) 
{
    int num[5000];
	int cut_i,max;
	int kousuu;
	int nyuryoku;

	while(1){
		scanf("%d",&kousuu);
		
		if(kousuu == 0){
			break;
		}

		int kou = 0;
		
		for(cut_i=0;cut_i<kousuu;cut_i++){
			num[cut_i]=0;
		}

		for(cut_i=0;cut_i<kousuu;cut_i++){
			scanf("%d",&nyuryoku);

			if(num[kou] * nyuryoku <0){
				kou++;
			}
			num[kou]+=nyuryoku;

			if(max<num[kou]){
				max = num[kou];
			}
		}

		kou++;
		cut_i = 0;
		
		for(cut_i=0;cut_i<kou;cut_i++){

			if(max<num[cut_i]){
				max = num[cut_i];
			}
			
			if(num[cut_i]+num[cut_i+1] > 0){
				num[cut_i+1]+=num[cut_i];
			}
		}

		printf("%d\n",max);

	}
    return 0;
}

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

int main() {
	int n;
	std::vector<int> a;
	while(std::cin >> n, n){
		for(;n--;){
			int ai;
			std::cin >> ai;
			a.push_back(ai);
		}
		long long int max_sum = a[0];
		for(int len=1; len<=a.size(); len++){
			for(int begin=0; begin<a.size()-len+1; begin++){
				long long int sum = 0;
				for(int i = begin; i < begin + len; i++){
					sum += a[i];
				}
				if(sum > max_sum){
					/*std::cout << begin << ',' << len << std::endl;*/
					max_sum = sum;
				}
			}
		}
		std::cout << max_sum << std::endl;
		a.clear();
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	while (1){
		int n, a[5001] = { 0 };
		cin >> n;
		if (n == 0)break;
		a[0] = 0;
		int maxv = -100000;
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		for (int i = 1; i <= n; i++){
			a[i] = max(a[i - 1] + a[i], a[i]);
			maxv = max(a[i], maxv);
			if (maxv < a[i])
				maxv = a[i];
		}
		cout << maxv << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n;
	while(cin >> n,n){
		vector<int> data(n);
		rep(i,n)cin >> data[i];
		int ret = 0;
		rep(i,n)ret = min(ret,data[i]);
		rep(i,n){
			int sum = 0;
			for(int j=i;j<n;j++){
				sum += data[j];
				ret = max(sum,ret);
			}
		}
		cout << ret << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){

	long long int A[100000]={0};
	long long int n,MAX = -100000;

		while(1){
			

			cin >> n;

			if( n == 0 ) break;

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

				cin >> A[i];
	
			}

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

				A[i] = max(A[i],A[i] + A[i-1]);
				MAX = max(A[i], MAX);

			}
	
			cout  << MAX << endl;

		}
	return 0;
}

C++: #include <iostream>

using namespace std;

/** Problem0022 : Maximum Sum Sequence **/

int main()
{
	int n, num[5000]={0}, s;
	int maxNum;
	
	while (1) {
		cin >> n;
		if (n==0) break;
		for (int i=0; i<n; i++) {
			cin >> num[i];
		}
		
		maxNum = 0;
		for (int i=0; i<n; i++) {
			s=0;
			for (int j=i; j<n; j++) {
				s+=num[j];
				if (s>maxNum)
					maxNum=s;
			}
		}
		cout << maxNum << endl;
	}
}