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>

int main() {
	int n;
	while (std::cin >> n) {
		if (n == 0) break;
		std::vector<int> a;
		int max = 0, left = 0, right = 0, lmin = 0, rmin = 0;
		for (int i = 0; i < n; ++i) {
			int ai;
			std::cin >> ai;
			a.push_back(ai);
			max += ai;
		}
		// ここをかく
		int lmax,rmax;
		std::vector<int>::iterator leftend = a.begin();
		std::vector<int>::reverse_iterator rightend = a.rbegin();
		for (std::vector<int>::iterator itr = a.begin(); itr != a.end(); ++itr) {
			left += *itr;
			if (left < lmin) {
				lmin = left;
				leftend = itr;
			}
		}
		for (std::vector<int>::iterator itr = a.end() - 1; itr != leftend; --itr) {
			right += *itr;
			if (right < rmin) {
				rmin = right;
			}
		}
		lmax = max - lmin - rmin;
		left = 0; right = 0; lmin = 0; rmin = 0;
		for (std::vector<int>::reverse_iterator ritr = a.rbegin(); ritr != a.rend(); ++ritr) {
			right += *ritr;
			if (right < rmin) {
				rmin = right;
				rightend = ritr;
			}
		}
		for (std::vector<int>::reverse_iterator ritr = a.rend() - 1; ritr != rightend; --ritr) {
			left += *ritr;
			if (left < lmin) {
				lmin = left;
			}
		}
		rmax = max - lmin - rmin;
		max = lmax > rmax ? lmax : rmax;

		std::cout << max << std::endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int num[5001];
/*
int sum(int a,int b){
  int s=0;
  for(int i=0;i<b;i++){
    s+=num[a+i];
  }
  return s;
}
*/
int main(){
  int max;
  int n,sum;
  
  while(cin>>n,n){
    max=-10000000;
    // int sum=-10000000;
    for(int i=0;i<n;i++){
      cin>>num[i];
    }
    for(int i=0;i<n;i++){
      sum=0;
      for(int j=i;j<n;j++){
	sum+=num[j];
	if(max<sum)max=sum;
      }
    }
    cout<<max<<endl;
  }
}

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

int main()
{
	int n;
	while (cin >> n, n)
	{
		int ary[5001] = {};

		for (int i = 1; i <= n; i++)
		{
			cin >> ary[i];
		}
		for (int i = 1; i <= n; i++)
		{
			ary[i] += ary[i - 1];
		}

		int ans = -1;
		for (int i = 0; i <= n; i++)
		{
			for (int j = i + 1; j <= n; j++)
			{
				ans = max(ans, ary[j] - ary[i]);
			}
		}
		cout << ans << endl;
	}

	return 0;
}

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

int main(){
	long long a, maximum;
	int n;
	while (cin >> n, n){
		maximum = -1000000;
		long long S[5010] = {};
		for (int i = 1; i <= n; i++){
			cin >> a;
			S[i] = S[i - 1] + a;
		}

		for (int i = 0; i <= n; i++)
			for (int j = i + 1; j <= n; j++){
				maximum = max(maximum, S[j] - S[i]);
			}
		cout << maximum << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define rep(i,n) REP(i, 0, n)
int a[5001];
int main(){
    int n;
    while(cin >> n && n){
        rep(i, n) cin >> a[i];
        int ans = -INF;
        REP(i, 0, n){
            int sum = a[i];
            ans = max(ans, sum);
            REP(j, i + 1, n){
                sum += a[j];
                ans = max(ans, sum);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

C++: #include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
if(n==0)
{
	break;
}
else
{
int a[n];
int A=0;
int R=-100000;
for(h=0;h<n;h++)
{
	cin>>a[h];
}
for(int i=0;i<n;i++)
{
	for(int j=0;j<n-i;j++)
	{
	for(int k=i;k<=j;k++)
	{
		A=A+a[k]
		
	}	
	if(R<A)
	{
		R=A;
	}
	A=0
	}
}
cout<<R<<endl;
}
return 0;
}

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

int main()
{
	int n, a[5000], i, j, k, l, sum, max;

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

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

		max = -500000000;
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n - i; j++)
			{
				for (k = 0; k < n - i; k++)
				{
					sum = 0;
					for (l = k; l <= i; l++)
						sum += a[l];
					if (sum > max) max = sum;
				}
			}
		}
		cout << max << endl;
	}
	return 0;
}

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

int main(){
    while (1){
	int n;
	int input[5001];
	int max1;
	cin >> n;
	if (n == 0) break;

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

	for (int i = 0; i < n; i++){
	    int sum = 0;
	    for (int j = i; j < n; j++){
		sum += input[j];
		max1 = max(max1, sum);
	    }
	}
	cout << max1 << endl;
    }
    return 0;
}

C++: #include <stdio.h>
int main()
{
	int n,a[5000];
	while(scanf("%d",&n)!=EOF){
		int max=-10001;
		if(n==0)
			break;
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		for(int i=0;i<n;i++){
			int sum=0;
			for(int j=i;j<n;j++){
				sum+=a[j];
				if(max<sum)
					max=sum;
			}
		}
		printf("%d\n",max);
	}
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<memory>
#include<array>
#include<vector>
#include<queue>
#include<list>
#include<stack>
#include<algorithm>
#include<cmath>

using namespace std;

int main()
{
  int a[5000];
  int n;
  
  while( cin >> n )
    {
      int mv = -200000;
      
      if ( n == 0 )
        break;

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

      unique_ptr<int[][5000]> c(new int[n][5000]);
      
      for ( int i = 0; i < n; ++i )
        {
          for ( int j = 0; j <= i; ++j )
            {
              if ( i == j )
                {
                  c[i][j] = a[i];
                }
              else
                {
                  c[i][j] = c[i - 1][j] + a[i];
                }
              mv = max( mv, c[i][j] );
            }
        }

      cout << mv << endl;
      
    }
  
  return 0;
}

C++: #include<iostream>
#include<cstring>
#define N 5001
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main()
{
    int n;
    while(cin >> n &&n)
    {
        if(n==0)break;
        long int s[N];
        //memset(s,0,sizeof(int)*N);
        int t;
        /*cin >> t;
        s[0]=t;max = t;min=t;*/
        s[0] = 0;
        long int max = 0;

        for(int i=0;i<n;i++){
            cin >> t;
            s[i+1] = s[i]+t;
            for(int j=0;j<i;j++)
            {
                if((s[i+1]-s[j])>max) max = s[i+1]-s[j];
            }
        }
        cout << max << endl;

    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <math.h>
#include <functional>
#include <algorithm>

typedef long long Int;
#define REP(i,n) for(int i = 0 ; i < n ; ++i)
using namespace std;

Int aa[10000];
Int dp[10000];

int main(){
	int n;
	while (cin >> n, n){

		REP(i, n){
			cin >> aa[i];
		}

		Int max_i = -999999999;
		dp[0] = max(dp[0], aa[0]);
		max_i = max(max_i, dp[0]);

		for (int i = 1; i < n; ++i){
			dp[i] = max(aa[i], dp[i - 1] + aa[i]);
			max_i = max(max_i, dp[i]);
		}
		cout << max_i << endl;
	}
	return 0;
}