Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func main() {
	sum := make([]int, 5001)
	for {
		n := nextInt()
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			a := nextInt()
			sum[i+1] = sum[i]+a
		}
		ret := sum[1]
		for i := 0; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				ret = max(ret, sum[j]-sum[i])
			}
		}
		fmt.Println(ret)
	}
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, const char * argv[])
{
    while (1) {
        int n; scanf("%d", &n);
        if (n == 0) break;
        long a[n], b[n], tmp = 0;
        for (int i = 0; i < n; i++) {
            scanf("%ld", &a[i]);
            b[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            for (int k = 0; k <= i; k++) {
                b[i] += a[k];
            }
        }
        for (int i = n-1; i > 0; i--) {
            for (int k = i-1; k >= 0; k--) {
                if (i == n-1) {
                    tmp = (b[i] - b[k] + a[k]);
                }
                if (tmp < (b[i] - b[k] + a[k])) {
                    tmp = (b[i] - b[k] + a[k]);
                }
            }
        }
        cout << tmp << endl;
    }
    return 0;
}

C++: #include <iostream>
int main(){
int a,b,c=1,d=0,z=-100000;
while(c=1){
	std::cin>>a;
	
	

	if(a==0){
		return 0;
	}
	int aa[a];
	for(int t=1;t<=a;t++){
		std::cin>>b;
	
		aa[t]=b;
	}
    
	for(int s=1;s<=a;s++){
		
			for(int l=s;l<=a;l++){
				d=d+aa[l];
				if(z<d){
					z=d;
				}
			}d=0;
		
		
	}
	std::cout<<z<<std::endl;
	z=0;
}
}

C++: #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
#define NMAX 5100

//int dp[NMAX][NMAX];
int a[NMAX];

int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)break;
        memset(a,0,sizeof(int)*NMAX);

        for(int i =1;i<=n;++i)
        {
            cin >> a[i];
        }
        int sum = 0,ans = 0;
        for(int first = 1;first<=n; ++first)
        {
            for(int last = first;last<=n;++last)
            {
                sum += a[last];
                if(ans<sum) ans = sum;
            }
            sum = 0;
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main(void){
	int n;
	int a[5000];
	while(cin>>n,n){
		for(int i=0;i<n;i++)cin>>a[i];
		int sum=0;
		int m=a[0];
		for(int i=0;i<n;i++){
			sum+=a[i];
			m=max(m,sum);
			sum= sum>=0 ? sum : 0;
		}
		cout<<m<<endl;
	}
	return 0;
}

C++: #include <stdio.h>

int main(void)
{
	int i;
	long max;
	long total;
	int a[5010];
	int n;
	
	while (scanf("%d", &n), n != 0){
		max = 0;
		total = 0;
		for (i = 0; i < n; i++){
			scanf("%d", &a[i]);
			total += a[i];
			if (max < total){
				max = total;
			}
			if (total < 0){
				total = 0;
			}
		}
		printf("%d\n", max);
	}
	return (0);
}

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

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <queue>
#include <map>
#include <fstream>
#include <array>
#include <utility>
#include <functional>
#include <stack>
#include <math.h>

using namespace std;
typedef long long Int;
#define REP(i , x) for(Int i = 0 ; i < x ; ++i)
typedef pair<int, int> P;


int main()
{
	int n;

	while (cin >> n,n){
		Int dp[10000];
		Int aa[10000];

		fill(aa, aa + 10000, 0);
		fill(dp, dp + 10000, 0);

		REP(i, n){
			cin >> aa[i];
		}

		Int max_i = -99999999;

		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<bits/stdc++.h>
using namespace std;
int main(){
  int n, sum[5001] = {};
  while(cin >> n, n){
    for(int i = 0; i < n; i++){
      int a;
      cin >> a;
      sum[i + 1] = sum[i] + a;
    }
    int ret = sum[1];
    for(int i = 0; i <= n; i++){
      for(int j = i + 1; j <= n; j++){
        ret = max( ret, sum[j] - sum[i]);
      }
    }
    cout << ret << endl;
  }
}

C++: //
//AOJ volume0 0022
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0022&lang=jp
//20170529
//640009E
//Yang Bo
//

#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> a(n, 0); //?´??????¨
    vector<int> b(n, 0); //????????¨
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int k = n - 1;
    while (a[k] < 0)
      k--;
    b[k] = a[k];
    for (int i = k - 1; i > -1; i--) {
      if (a[i] < 0)
	continue;
      else {
	int j = i + 1;
	int s = 0; //?????????????????????????????¨
	while (a[j] < 0) {
	  s += a[j];
	  j++;
	}
	//cout << "s = " << s << " " << "i = " << i << " " << "j = " << j << endl;
	if (s + b[j] >= 0)
	  b[i] += s + b[j] + a[i];
	else // if (a[i] > s + b[j])
	  b[i] += a[i];
	//cout << "b[" << i << "]= " << b[i] << endl;
      }
    }
    /*
    for (int i = 0; i < n; i++)
      cout << "b[" << i << "]= " << b[i] << endl;
    */
    int max = b[0];
    for (int i = 1; i < n; i++)
      if (max < b[i])
	max = b[i];
    if (max < 1) {
      int minus = a[0];
      for (int i = 1; i < n; i++)
	if (minus < a[i])
	  minus = a[i];
      max = minus;
    }
    cout << max << endl;
  }
}

/*
??????????????????????????£???????????§??????????????????wrong answer???????????§??????
????????§?????£???????????¨???????????§?????????????????????????????????
?????????????????§????????§????±?????????????(??????b?????\????????????????????????)?????¨????????°?????????????±?????????¨????????¢?????´?????????????????£???????????????
????????£???????¬??????????????¨?????????¨?¨????????????§???????????????
*/

C++: #include <iostream>
#include <algorithm>
const int MAX_N = 5000;
using namespace std;

int a[MAX_N];

int main(void) {
  int n;
  
  while (cin >> n) {
    if (n == 0) break;
    long long max_num = -500000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n-i; j++) {
	long long tmp = 0;
	for (int k = i; k <= j; k++) {
	  tmp += a[k];
	}
	max_num = max(max_num, tmp);	
      }
    }

    cout << max_num << endl;
  }

  return 0;
}

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

int a[5000] = {0};
int S[5000] = {0};

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

  S[0] = a[0];
  for(int i = 1; i < n; i++) {
    S[i] += S[i-1] + a[i];
  }
  

  int maxsum = 0;
  int sum = 0;
  for(int i = 1; i <= n-1; i++){
    for(int j = i + 1; j <= n;j++) {
      sum = S[j] - S[i];
      if(sum > maxsum)
	maxsum = sum;
      sum = 0;
    }
  }
  cout << maxsum << endl;
  }
}

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

int main(void) {
  int n, a[5001];
  long long int sum, mx;
  
  while(cin>>n && n) {
    for(int i = 0; i < n; i++) cin >> a[i];

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

  return 0;
}