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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;
    while(cin>>n,n){
        vi v(n+1,0);
        rep(i,n)cin>>v[i];
        ll ans=0;
        for(int i=0;i<n;i++){
            ll temp=0;
            for(int j=i;j<n;j++){
            for(int k=i; k<=j;k++)temp+=v[k];
            ans=max(ans,temp);
                temp=0;
            }
        }
        cout<<ans<<endl;
    }
}

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

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++: //0022 Maximum Sum Sequece

//2<=n<=5000ÂÌñ©çA×Ú·éÌaÌÅålð¦æ
//È¨AÌlÍ-10`10ÌÍÍÅ é
//KÉâÁ½ç°i(1¨n-1){i*(n-i)}ñÅ]TÅlÞ©
//Æè ¦¸Ag¤ñÌÉV½ÉÁíÁ½àÌð«µÄAÁ¦éàÌðø­ûjÅ¢¢©


#include<iostream>
#include<algorithm>
#include<numeric>
#include<climits>
using namespace std;

int main(void)
{
	const int N = 5000;
	int n;
	while(cin >> n, n)
	{
		int i, j;
		int a[N];
		int sum, max_sum = INT_MIN;

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

		//iÂÌðg¤
		for(i = 1; i <= n; i++)
		{
			//Ü¸ÍAÜÆÜÁÄg¤ªÌaðvZµÄ¨­
			sum = accumulate(a, a+i, 0);
			max_sum = max(sum, max_sum);

			for(j = 0; j+i < n; j++)
			{
				//ÌÂðg¢Ü·
				sum -= a[j];
				sum += a[j+i];
				max_sum = max(max_sum, sum);
			}
		}

		cout << max_sum << 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+100);
    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<algorithm>

#define MAX_N 5000

int main(){
    int n, ans = 0, in, before;
    while(std::cin >> n, n){
        std::cin >> before;
        ans = before;
        for(int i = 1; i < n; ++i){
            std::cin >> in;
            before = std::max(in, before + in);
            ans = std::max(before, ans);
        }
        std::cout << ans << std::endl;
        ans = 0, before = 0;
    }
    return 0;
}

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

int main(){
	int i, j, k, n, sum, result;
	int a[5000];
	while(cin >> n){
		if(n == 0){
			break;
		}
		result = 0;
		for (i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (i = 0; i < n; i++) {
			for (j = 1; j <= n-i; j++) {
				sum = 0;
				for (k = 0; k < j; k++) {
					sum += a[i+k];
				}
				if(result < sum){
					result = sum;
				}
			}
		}
		cout << result << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int count,max,i,j,k,l;
   int sum,data_set[5000];
  while(cin>>count){
    if(!count)break;
    cin>>data_set[0];
    max=data_set[0];
    for( i=1;i<count;i++){
      cin>>data_set[i];
    }
    //  for(l=1;l<=count;l++){
      for( j=0;j<count;j++){
	sum=0;
	for( k=j;k<count;k++){
	  sum+=data_set[k];
	if(max<sum)max=sum;
	}
    }
    cout<<max<<endl;
  }
}

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;i++)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<vector>
#include<algorithm>

using namespace std;

#define FOR(a) for(int i = 0; i < a; i++)
#define FORR(a, b) for(int i = a; i < b; i++)

int main(void){
	int Max, Num = -1, In, N = 0;
	vector<int>	Dat;
	while (Num != 0){
		Max = -100000;
		cin >> Num;
		Dat.push_back(-100000);
		FOR(Num){
			cin >> In;
			Dat.push_back(In);
		}
		FORR(1, Num + 1){
			Dat[i] = max(Dat[i], Dat[i - 1] + Dat[i]);
			Max = max(Max, Dat[i]);
		}
		N = Dat.max_size();
		FOR(N){
			Dat.pop_back();
		}
		cout << Max << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algortihm>
using namespace std;
int main(){
	long int n, m[5001] = { 0 }, max = -2147483647, temp = 0, k;
	while (cin>> n && n!=0){
		for (int i = 0; i < n; i++){
			cin >> m[i];
		}
		for (int i = 0; i < n; i++){
			temp = m[i];
			for (int l = i + 1; l < n; l++){
				temp += m[l];
				if (temp > m[i]) if (max < temp) max = temp;
				else if (max < m[i]) max = m[i];
			}temp = 0;
		}
		cout << max << endl;
		max = -2147483647;
	}
	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; 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;
						}
						break;
					}
				}
			}
			if(max < tmpmax2){
				max = tmpmax2;
			}
		}
		delete [] factor;
		cout << max << endl;
	}
	
	return 0;
}