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>

using namespace std;

int main()
{
	int n;

	while (1)
	{
		int a[5001] = {0}, s[5001] = {0}, max = 0;

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

		for (int i=1; i<=n; i++)	//入力
			cin >> a[i];

		for (int i=1; i<=n; i++) {
			for (int j=1; j<=i; j++) {
				s[i] += a[j];		//s[i] = a[1]+a[2]+...+a[i]
			}
		}

		//cout << s[0] << endl;	//Debug

		//int cont = 0;	//Debug
		for (int i=1; i<=n; i++) {
			for (int j=1; j<=i; j++) {
				//if (max < s[i] - s[j-1]) cout << "max = " << s[i] - s[j-1] << endl;	//Debug
				max = (max < s[i] - s[j-1]) ? s[i] - s[j-1] : max;	//区間i,jの和を求めて比較
				//cont++;	//Debug
			}
		}

		cout << max << endl;
		//cout << cont;
	}

	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){

		fill(aa, aa + 10000, 0);
		fill(dp, dp + 10000, 0);
		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;
}

C++: #include <stdio.h>
#include <iostream>
#include <queue>
#include <map>
#include <string>
#include <vector>
#define PB push_back
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
using	namespace std;
int n,tmp;
void solve(){
	while(cin >> n  && n!= 0){
		vector <int> data ;
		REP(i,n){
			cin >> tmp;
			data.PB(tmp);
		}
		int sofar = 0 ,end = 0;
		REP(i,n){
			end = max(end+data[i],0);
			sofar = max(end,sofar);
		}
		if(sofar == 0){
			sofar = -10000000;
			REP(i,n)sofar = max(sofar,data[i]);
		}
		cout << sofar <<endl;


	}

}

int main(){
	solve();

}

C++: #include <iostream>

using namespace std;

int main(){
  int n;
  while(true){
    cin>>n;
    if(!n)break;
    int a[n],b[n];
    for(int i=0;i<n;i++){
      cin>>a[i];
      b[i] = 0;
    }
    int k=0;
    for(int i=0;i<n;){
      while(i<n&&a[i]>0){
	b[k] += a[i];
	i++;
      }
      k++;
      if (i>=n)break;
      while(i<n&&a[i]<=0){
	b[k] += a[i];
	i++;
      }
      k++;
    }
    int max = 0;
    for(int i=0;i<k;i++){
      for(int j=i;j<k;j++){
	int sum = 0;
	for(int l=i;l<=j;l++)
	  sum+=b[l];
	if(sum>max)max = sum;
      }
    }
    cout << max << endl;
  }
  return 0;
}

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

#define MAX(X,Y) ((X)>(Y)?(X):(Y))

int main(void){
  long long ans, acc[5001];
  int n;
  
  while (cin >> n){
    if (n==0) break;
    acc[0] = 0;
    for (int i=0; i<n; i++){
      int t;
      cin >> t;
      acc[i+1] = t + acc[i];
    }
    ans = -(1<<31-1);
    for (int i=0; i<n; i++){
      for (int j=0; j<=i; j++){
        int sum = acc[i+1] - acc[j];
        ans = MAX(ans, sum);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;
int a[5010];
int dp[5010];
int main(){
	while(cin>>n,n){
		for(int i =0;i < n;i++){
			cin>>a[i];
			dp[i+1]=a[i];
		}
		dp[0] = 0;
		int ans = -(1<<25);
		for(int i =1;i <= n;i++){
			dp[i] =max(dp[i-1]+a[i-1],dp[i]);
			ans =max(ans,dp[i]);
		}
		cout <<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main( void )
{
	while (1)
	{
		int n;
		cin >> n;
		if ( n == 0 )
			break;

		bool flag = false;
		int max_value = 0;
		int now = 0;
		for ( int i = 0; i < n; i++ )
		{
			int value;
			cin >> value;
			now += value;
			if ( flag == false )
			{
				max_value = now;
				flag = true;
			}
			max_value = max( now, max_value );
			if ( now < 0 )
				now = 0;
		}

		if ( now > 0 )
			max_value = max( now, max_value );

		cout << max_value << endl;

	}
	return 0;
}

C++: #include <iostream>
#include <limits.h>
using namespace std;
#define N 5000

int main(void)
{
	while(1) {
		int n;
		long an[N] = {0}, sum = 0, max = LONG_MIN;
		cin >> n;

		if(n == 0)	break;

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

		for(int i = 0; i < n; i++) {
			sum = 0;
			for(int j = i; j < n; j++) {
				sum += an[j];
				if(sum > max) {
					max = sum;
				}
			}
		}

		cout << max << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;

ll dp[5000];
int main(){
	memset(dp,0, sizeof(dp));
	int n,a;
	while(1){cin >> n;if(!n)break;
		ll ans=0;
		
		rep(i,n){cin>>a;
			if(dp[i-1]>0)dp[i]=dp[i-1]+a;
			else dp[i] = a;
			ans = max(ans,dp[i]);
		}
		cout<<ans<<endl;
	}

}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
    while(1){
        int n;
        cin>>n;
        if(n==0){
            return 0;
        }
        vector<int> a(n);
        for(int i=0;0<n;i++){
            cin>>a.at(i);
        }
        vector<int> b(n+1);
        b.at(0)=0;
        for(int i=1;i<=n;i++){
            b.at(i)=b.at(i-1)+a.at(i-1);
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=0;j<i;j++){
                ans=max(ans,b.at(i)-b.at(j));
            }
        }
        cout<<ans<<endl;
    }
}