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;
	long max, sum, ans=-1000000;
	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>
#define _USE_MATH_DEFINES
#include<math.h>
#include<iomanip>
#include<string>
#include<algorithm>
using namespace std;

int gcd(long long int m,long long int n){
	while(m!=n){
		if(m>n){m=m-n;
		}
		else n=n-m;
	}
	return m;
}
int lmc(long long int m,long long int n){
return m*n/gcd(m,n);
}

int main(){
int n;
while (cin>>n,n!=0){
	int a[5000];
	for (int i=0;i<n;i++){
		cin>>a[i];
	}
	int temp=-100000;
	for (int i=1;i<=n;i++){
		for (int j=0;j<=n-i;j++){
			int sum=0;
			for (int k=0;k<i;k++){
				sum+=a[j+k];
			}
			if (temp<sum){
				temp=sum;
			}

		}

	}
	cout<<temp<<endl;
}

}

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

int main() {
    vector<int> v;
    int n, a, sum;
    while (cin >> n && n != 0) {
        v.clear();
        for (int i=0; i<n; ++i) {
            cin >> a;
            v.push_back(a);
        }

        int l = 0;
        sum = 0;
        for (int i=0; i<n; ++i) {
            sum += v[i];
            if (sum < 0) {
                sum = 0;
                l = i+1;
            }
        }

        int r = n-1;
        sum = 0;
        for (int i=n-1; i>=0; --i) {
            sum += v[i];
            if (sum < 0) {
                sum = 0;
                r = i-1;
            }
        }

        sum = 0;
        for (int i=l; i<=r; ++i) {
            sum += v[i];
        }

        cout << sum << endl;
    }
    return 0;
}

C++: #include<stdio.h>

int main(void)
{
  int a[5010]={};
  int tr;
  int i,j;
  int n;
  int max;

  for(scanf("%d",&n);n;scanf("%d",&n))
    {
      for(i=0;i<5010;i++)a[i]=0;
      max=0x80000000;
      for(i=1;i<=n;i++)
        {
          scanf("%d",&tr);
          a[i]=a[i-1]+tr;
        }
      for(i=0;i<n;i++)
        for(j=i;j<=n;j++)
          {
            tr=a[j]-a[i];
            if(max<tr)
              max=tr;
          }
      printf("%d\n",max);
    }

}

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

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MAX_N 5001
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<string, ll> PS;
typedef vector<ll> V;

int n, a[MAX_N], sum, MAX;

int main(){
	while (cin >> n&&n){
		MAX = -100000000;
		REP(i, n)cin >> a[i];
		for (int i = 0; i < n; i++){
			sum = a[i];
			MAX = max(MAX, sum);
			for (int j = i + 1; j < n; j++){
				sum += a[j];
				MAX = max(MAX, sum);
			}
		}
		cout << MAX << endl;
	}
}

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];

ll sum(int i,int j){
	ll s=0;
	rep(k,j){
		s+=a[i+k];
	}
	return s;
}

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

C++: #include <iostream>

using namespace std;

int main()
{
	int n;

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

		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]
			}
		}

		max = s[1];

		//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 << j << '-' << i << ", 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 << endl;
	}

	return 0;
}

C++: #include <stdio.h>

int main(void) {
  while( 1 ) {
  long long n, ans = 0, now = 0, i;
  scanf("%lld", &n);
  if(!n) break;
  int a[n];
  for(i = 0; i < n; ++i) scanf("%d", &a[i]);
  for(i = 0; i < n; ++i) {
    now += a[i];
    if(now > ans) ans = now;
    if(now < 0) now = 0;
  }
  if(!ans) {
    int max = -1000000;
    for(i = 0; i < n; ++i) if(max < a[i]) max = a[i];
    ans = max;
  }
  printf("%lld\n", ans);
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF 1<<27
using namespace std;
int main(){
  int n;
  while(cin >> n,n) {
    int sum[5000] = {0},a[5000];
    for(int i = 0 ; i < n ; i++ ) {
      cin >> a[i];
      if(i == 0) sum[i] += a[i];
      else  sum[i] += a[i] + sum[i-1];

    }
    int mx = sum[0];
    for(int i = 0 ; i < n-1 ; i++ ){
      for(int j = i+1 ; j < n ; j++ ){
	mx = max(mx,max(sum[j] - sum[i],sum[j]));
      }
    }
    cout << mx << endl;
  }
  return 0;
}

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