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 <algorithm> 
using namespace std;

int main(){
	int n;
	cin >> n;
	while(n != 0){
		int a[n];
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		int maximum = 0;
		int sum = 0;

		for(int i = 0; i < n; i++){
			for(int j = 0; j <= n - i; j++){
				for(int k = 0; k < j; k++){
					sum += a[i + k];
				}
				maximum = max(maximum, sum);
				sum = 0;
			}
		}
		cout << maximum << endl;
		cin >> n;
	}

	return 0;
}

C++: #include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000001

using namespace std;
#define ll long long
#define dbg if(0)



int main(){
    while(true){
        int n;
        ll ans= LLONG_MIN;
        cin>>n;
        if (n==0)break;

        ll rui[n+1];
        rui[0]=0;
        for(int i=1;i<=n;i++){
            cin >>rui[i];
            rui[i]+=rui[i-1];
            dbg cout <<rui[i]<<" ";
        }
        dbg cout <<endl;
        //次を見る
        //今までの最小値を更新したか？
        //したなら暫定解を確定しansと比較。最大値=最小値=発見した最小値にする
        //してなければ最大値の更新をする
        //見る場所が一番後ろまで行ったら終了。
//        rui[0]=min(rui[0],rui[1]);
//        ll nmin=rui[0],nmax=rui[0];
//        bool nmflag=false;
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                ans=max(rui[j]-rui[i-1],ans);
            }
        }
 /*       for(int i=0;i<=n;i++){
            if (rui[i]<nmin){
                ans=max(nmax-nmin,ans);
                nm=max(rui[i]-nmin,nm);
                nmax=rui[i];
                nmin=rui[i];
            }else{
                if(rui[i]>nmax){
                    nmax=rui[i];
                    nmflag=true;
                }
            }
            dbg cout <<nmax<<" "<<nmin<<" "<<nm<<endl;
        }
        ans=max(nmax-nmin,ans);
        dbg cout << endl;
        if(nmflag)cout<<ans<<endl;
        else cout<<nm<<endl;*/
        cout <<ans<<endl;
    }

    return 0;
}

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

int main() {
	int n;
	int max;
	while(1) {
		cin >> n;
		if(n == 0) break;
		int a[5010];
		for(int i = 0; i < n; i++) {
			cin >> a[i];
		}
		max = a[0];
		for(int i = 0; i < n; i++) {
			int temp = 0;
			for(int j = i; j < n; j++) {
				temp += a[j];
				if(temp > max) max = temp;
			}
		}
		cout << max << endl;
	}
	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;
const int MIN_A = -100000;

int max_sum[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[i] << " ";
        }
        cout << endl;

        cout << *max_element(max_sum + 1, max_sum + n + 1) << endl;
    }

    return 0;
}

void
update(int i, int a)
{
    max_sum[i] = max(max_sum[i - 1] + a, a);
}

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  int n,i,j;
  cin >> n;
  while(n!=0) {
    long long sum[5001]={};
    int b;
    vector <long long> v;
    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++){
	v.push_back(sum[j]-sum[i]);
      }
    }
    sort(v.begin(),v.end());
    cout << v[v.size()-1] << endl;
    cin >> n;
  }
  return 0;
  
}

C++: #include<bits/stdc++.h>
using namespace std;

int a[5001]={0};

int main(){
    int n;
    while(cin >> n, n != 0){
        for(int i=0;i<n;++i)cin >> a[i];
        int maxi = INT_MIN;
        for(int i=0;i<n;++i){
            int sum = a[i];
            if(sum > maxi)maxi = sum;
            for(int j=1;i+j<n;++j){
                sum += a[i+j];
                if(sum > maxi)maxi = sum;
            }
        }
        cout << maxi << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime>

using namespace std;

int main(void){
	const int INF=-100001;
	int n;
	int a,b;
	int num[5001];
	int ans=INF,ans2=0;
	while(1){
		ans=INF,ans2=0;
		cin >> n;
		if(n==0)break;
		for(a=0;a<n;a++)cin >>num[a];
		for(b=0;b<n;b++){
			ans2=0;
			for(a=b;a<n;a++){
				ans2+=num[a];
				ans=max(ans,ans2);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <stack>
#include <cstring>

using namespace std;

int main(){
	int n;
	int max,tmpmax1,tmpmax2;
	while(cin >> n && n != 0){
		max = -100001;
		int *factor = new int[n];
		
		for(int i = 0; i < n; i++){
			cin >> factor[i];
		}

		for(int i = 0; i < n - 1; i++){
			tmpmax1 = 0;
			tmpmax2 = -100001;
			for(int j = i; j < n; j++){
				tmpmax1 += factor[j];
				if(tmpmax2 < tmpmax1){
					tmpmax2 = tmpmax1;
				}
			}
			if(tmpmax2 < 0){
				for(int i = 0; i < n; i++){
					if(tmpmax2 == factor[i]){
						if(i != n - 1 && factor[i + 1] <= 0){
							tmpmax2 += factor[i + 1];
							break;
						}
					}
				}
			}
			if(max < tmpmax2){
				max = tmpmax2;
			}
		}
		delete [] factor;
		cout << max << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include <algorithm>


using namespace std;

int main(){
	int n;
	int max1;
	int maxS;
	int sum=0;
	int sumS=0;
	int  raw[5000];
	int ans;
	while(cin>>n,n!=0){
		sum=0;
		maxS=0;
		sumS=0;
		cin >> raw[0];
		max1=raw[0];
		sum += raw[0];
		if(sum<0)sum=0;
		for(int i=1;i<n;i++){
			cin >> raw[i];
			if(max1<raw[i]) max1 = raw[i];
			if(raw[i]<0){
				if(sumS<sum) sumS=sum;
			}
			sum += raw[i];
			if(sum<0){
				if(maxS<sum)maxS=sum;
				sum=0;
			}
		}
		ans=max(sum,max(max(sumS,max1),maxS));
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<iostream>

int n;
int a[1000];

void add( int i, int x )
{	
	while( i <= n ){
		a[i] += x;
		i += i & (-i);
	}
	
	return;
}

int sum( int i )
{
	if( i == 0 ) return 0;

	int s = 0;
	while( i > 0 ){
		s += a[i];
		i -= i & (-i);
	}
	
	return s;
}

int main()
{
	while( 1 ){
		memset( a, 0, sizeof( a ) );
	
		std::cin >> n;
		if( !n ) break;
		
		for( int i = 1; i <= n; i++ ){
		 	int x;
			std::cin >> x;
			add( i, x );
		}
		
		int ans = 0;
		
		for( int i = 0; i < n; i++ ){
		 	for( int j = i + 1; j <= n; j++ ){
				ans = std::max( ans, sum( j ) - sum( i - 1 ) );
			}
		}
		
		std::cout << ans << std::endl;
	}
	
 	return 0;
}