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

int main() {
	int n, max, sum, ans;
	while (true) {
		cin >> n;
		if (n == 0) break;
		vector<int> array(n);
		for (int i = 0;i < n;i++) {
			cin >> array[i];
		}

		for (int j = 0;j < n - 1;j++) {
			max = array[j] + array[j + 1];
			sum = array[j];
			for (int i = j + 1;i < n;i++) {
				sum += array[i];
				if (sum > max) {
					max = sum;
				}
			}
			if (j == 0) {
				ans = max;
			}else if(max > ans){
				ans = max;
			}
		}
		if (array[n - 1] > ans) {
			ans = array[n - 1];
		}


		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	while(1){
		int n;
		int a[5001];
		cin>>n;
		if(n==0)break;
		int b;
		a[0]=0;
		cin>>a[1];
		for(int i=2;i<=n;i++){
			cin>>b;
			a[i]=a[i-1]+b;
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<i;j++){
				ans=max(ans,a[i]-a[j]);
			}
		}
		cout<<ans<<endl;
	}
}

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

int main(){
  int n;
  int a[5000];
  int tmp;
  int ans;

  while(1){
    cin >> n;
    if(!n)break;

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

    ans = a[0];
    for(int i=0;i<n;i++){
      for(int j=i;j<n;j++){
	if(i==j)tmp = a[i];
	else tmp += a[j];
	ans = max(tmp,ans);
      }
    }

    cout << ans << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace	std;
int main(){

  for(int n;cin>>n&&n;){
    int a[5000],r;
    cin>>a[0];
    r=a[0];
    for(int i=1,b;i<n;i++){
      cin>>b;
      a[i]=a[i-1]+b;
      r=max({r,a[i],b});
    }
    for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) r=max(r,a[j]-a[i]);
    cout<<r<<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;
int sum[5001];
int main(){
	int N;
	sum[0] = 0;
	while(cin>>N&&N){
		int index=0;
		int ans = 0;
		
		FOR(i,1,N+1){
			cin>>sum[i];
			sum[i] += sum[i-1];
		}
		FOR(i,1,N+1){
			FOR(j,i,N+1){
				ans = max(ans,sum[j]-sum[i-1]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: 
#include<iostream>

using namespace std;

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

C++: #include<iostream>
int n[5000];
int main(){
	while(1){
		int a;
		std::cin >> a;
		if(a==0)return 0;
		int dp[5000];
		for(int i = 0; i < a; ++i){
			std::cin >> n[i+1];
		}
		int max = 0;
		dp[0]=n[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];
		}
		std::cout<<max<<std::endl;
	}
}

C++: #include <iostream>

int main(){
  int n, num;

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

    num = 0;

    for(int i=0;i<n;i++){
      cin >> n;
      num += n;
      if(num<0) num = 0;
    }

    cout << num << endl;
  }

  return 0;
}

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

int main()
{
	int a;
	while(1){
		cin>>a;
		if(a==0){
			break;
		}
		int n[5000];
		for(int i=0;i<a;i++){
			cin>>n[i];
		}
		int max_ret=0;
		int dp[5000];
		dp[0] = n[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];
			}
			max_ret = max( dp[i], max_ret);
		}
		
		cout<<max_ret<<endl;
	}
	
	
	return 0;
}

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

using namespace std;

int main() {
  int n;
  vector<int> datas;
  vector<int> sums;

  while (cin >> n, n != 0) {
    int input;
    for (int n_i = 0; n_i < n; n_i++) {
      cin >> input;
      datas.push_back(input);
    }
    for (int data_i = 0; data_i < n-1; data_i++) {
      sums.push_back(datas[data_i]+datas[data_i+1]);
    }
    cout << *max_element(sums.begin(), sums.end()) << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
ll a[5000];


int main(){
	while(cin>>n,n!=0){
		int c=0;
		ll MAX=-10000000;
		cin>>a[0];
		rep(i,n-1){
			int t;
			cin>>t;
			if(a[c]*t<0){
				a[++c]=t;
			}
			else a[c]+=t;
		}
		
		rep(i,c+1){
			ll max1=-100000000,max2=-100000000;
			ll t=0;
			for(int j=i-1;j>-1;j--){
				t+=a[j];
				if(max1<t){
					max1=t;
				}
			}
			if(max1==-100000000) max1=0;
			t=0;
			for(int j=i+1;j<c+1;j++){
				t+=a[j];
				if(max2<t){
					max2=t;
				}
			}
			if(max2==-100000000) max2=0;
			if(max1<0) max1=0;
			if(max2<0) max2=0;
			//cout<<max1<<" "<<max2<<endl;
			if(MAX<a[i]+max1+max2) MAX=a[i]+max1+max2;
			//cout<<endl;
		}
		cout<<MAX<<endl;
		
		//rep(i,c+1) cout<<a[i]<<endl;
		//cout<<endl;
	}
	
	return 0;
}

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

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