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>
#include <climits>
#include <vector>
using namespace std;
typedef long long llong;
typedef vector <llong> vll;

int maxv(vll &x){
	llong tmp=x[0];
	for (int i=1; i<x.size(); i++)
		if (x[i]>tmp) tmp=x[i];
	return tmp;
}

int main(void){
	int n;
	vll x;
	while (cin>>n, n!=0){
		llong s = LLONG_MIN;
		for (int i=0; i<n; i++){
			int n2;
			cin>>n2;

			for (int j=0; j<x.size(); j++){
				x[j] += n2;
			}
			x.push_back(n2);
			llong tmp=maxv(x);
			if (tmp>s) s=tmp;
		}
		cout<<s<<endl;
		x.clear();
	}
	return 0;
}

C++: /*
 * AOJ-Volume0-0022
 */
#include <iostream>
#include <limits.h>
#include <vector>
#include <cstdio>

using namespace std;

typedef long long ll;

/*
 * Find maximum sum.
 *   array: number list
 */
ll findMaximumSum(vector<int> &array){
  ll sumValue = array[0];
  ll maxValue = array[0];
  int length = array.size();

  for(int i = 1; i < length; i++){
    ll value = array[i];

    if(sumValue < 0){
      sumValue = value;
    }else{
      sumValue += value;
    }

    if(maxValue < sumValue){
      maxValue = sumValue;
    }
  }

  return maxValue;
}

void solve(int n){
  vector<int> a(n);

  for(int i = 0; i < n; i++){
    scanf("%d", &a[i]);
  }

  cout << findMaximumSum(a) << endl;
}

int main(){
  int n;

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

    solve(n);
  }

  return 0;
}

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

int main() {
	int n;
	vector<int> r;
	while (true)
	{
		scanf("%d", &n);
		if (n==0)break;
		int max=INT_MIN,now=0,in=0;
		for (int i=0; i<n; i++)
		{
			cin >> in;
			now += in;
			if (now > max) max=now;
			if (now < 0) now=0;
		}
		r.push_back(max);
	}
	for (int i=0; i<r.size(); i++)
	{
		cout << r[i] << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define LOOP(i,x,n) for(int i=x;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long

using namespace std;
const int MOD=1000000007;
const int INF=9000000009;
const double eps=1e-10;

signed main(){
  int n;
  while(cin>>n,n){
    vector<int> a(n);
    REP(i,n){
      int x;cin>>x;
      a[i+1]=a[i]+x;
    }
    int ans=-INF;
    REP(i,n+1)LOOP(j,i+1,n+1)ans=max(ans,a[j]-a[i]);
    cout<<ans<<endl;
  }
  return 0;
}

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

using namespace std;

int debug = 0;

int main(void){
	int n, m, i, j, k;
	int tmp, max;
	bool first = 0, notAllMinus=0;
	vector<int> intv;

	while(cin >> n){
		if(n == 0){
			break;
		}else{
			//ÇÝÝ
			for(i=0;i<n;i++){
				cin >> m;
				intv.push_back(m);
				//if(debug) cout << "int[" << i << "] =" << intv[i] << endl; 
			}

			//SÌêÌ»è
			tmp = intv[0];
			for(i=0;i<intv.size();i++){
				if(intv[i] > 0){
					notAllMinus = 1;
					break;
				}else{
					if(tmp < intv[i]){
						tmp = intv[i];
					}
				}
			}

			if(notAllMinus == 0){
				cout << tmp << endl;
				intv.clear();
				continue;
			}

			//vfð¸ç·
			tmp=0;
			for(i=1;i<intv.size();i++){
				if(intv[tmp] > 0){
					if(intv[i] > 0){ //¯Èç
						intv[tmp] += intv[i];
						intv[i] = 0;
					}else{ //ÙÈç
						tmp = i;
					}
				}else if(intv[tmp] < 0){
					if(intv[i] < 0){
						intv[tmp] += intv[i];
						intv[i] = 0;
					}else{
						tmp = i;
					}
				}
			}
			
			if(debug){
				for(i=0;i<intv.size();i++){
					cout << intv[i] <<endl;
				}
				cout << "----------------------" << endl;
			}
			
			intv.erase( remove(intv.begin(), intv.end(), 0), intv.end());
			
			if(debug){
				for(i=0;i<intv.size();i++){
					cout << intv[i] <<endl;
				}
			}
			

			int vsize = intv.size();
			for(i=1;i<=vsize;i+=2){//i:Â
				if(debug) cout << "number = " << i << endl;
				for(j=0;j<=vsize-1-i+1;j++){ //j:JnÊu
					tmp = 0;
					for(k=0;k<i;k++){
						tmp += intv[j+k];
					}
					if(debug) cout << "tmp == " << tmp << endl;
					if(first == false){
						max = tmp;
						first = true;
					}else if(max < tmp){
						max = tmp;
					}
				}
			}
			cout << max << endl;
			intv.clear();
			first = false;
			notAllMinus = 0;
		}
	}
	return 0;
}

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

using namespace std;

int main(){
  vector<int> sum;
  int n;
  int input;

  while(cin>>n){
    if(n==0)break;
    vector<int> array;
    for(int i=0;i<n;i++){
      cin>>input;
      array.push_back(input);
    }
    vector< vector<int> > now;
    vector<int> maxarray;
    for(int i=0;i<n;i++){   //start point
      for(int k=i;k<n;k++){  //end point
	int a=0;
	for(int j=i;j<=k;j++){
	a+=array[j];
      }
      now[i].push_back(a);
      }
    }
    for(int i=0;i<n;i++){
      int ma=0;
      for(int j=i;j<n;j++){
	if(i<n-1){
	  ma=max(now[i][j],now[i][j+1]);
	}
      }
      maxarray.push_back(ma);
    }
    int ma1;
    for(int i=0;i<n;i++){
      if(i<n-1){
	ma1=max(maxarray[i],maxarray[i+1]);
      }
    }
    sum.push_back(ma1);
  }
  vector<int> ans;
  int i=0;
  int size=sum.size();
  while(!sum.empty()){
    ans.push_back(sum[size-1-i]);
    sum.pop_back();
    i++;
  }
  i=0;
  while(!ans.empty()){
    cout<<ans[size-1-i]<<"\n";
    ans.pop_back();
    i++;
  }
  return 0;
}

C++: #include<iostream>
main(){int n,v[5000],m;while(std::cin>>n,m=n){for(;m--;)std::cin>>v[m];m=*v;for(int i=0;i++<n;)for(int j=i,k=v[i];j++<n;k+=v[j])m=k>m?k:m;std::cout<<m<<'\n';}}

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

long long a[5000];

int main(){
	int n;
	while(cin >> n && n){
		long long ans = -1e9;
		for(int i = 0 ; i < n ; i++) cin >> a[i] , ans = max(ans, a[i]);
		long long sub = 0;
		for(int i = 0 ; i < n ; i++){
			sub += a[i];
			if( sub < 0 ){
				sub = 0;
			}else{
				ans = max(ans,sub);
			}
		}
		cout << ans << endl;
	}
}