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 <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>

using namespace std;


#define all(c) ((c).begin()), ((c).end())
#define debug(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back


const double EPS = 1e-10;


typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef complex<double> P;


int main(){
	for (int n; cin >> n, n != 0; ) {
		vi a(n);
		rep(i, n) cin >> a[i];
		int ans = 0;
		rep(i, n) {
			int sum = 0;
			REP(j, i, n - 1) ans = max(ans, sum += a[j]);
		}
		cout << ans <<endl;
	}
	
	return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MAXN 5010
int main(void) {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        int a[MAXN], sum[MAXN];
        for (int i = 0; i < n; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
        sum[0] = 0;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            sum[i] = sum[i-1] + a[i];
        }
        int ret = a[1];
        for (int i = 0; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                ret = max(sum[j]-sum[i], ret);
            }
        }
        cout << ret << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
 
typedef long long ll;
typedef pair<int, int> ii;

#define EPS 1e-10

int main(){

	int n;
	while(cin >> n, n){
		vector<int> a(n);
		rep(i, n) cin >> a[i];
		
		ll sum = 0;
		ll ans = -LLINF;
		rep(i, n){
			sum += a[i];
			ans = max(ans, sum);
			if(sum < 0) sum = 0;
		}
		
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

#define N 2<<30;

int main(){

  int n;


  while(cin >> n){
    if(n == 0) break;
    vector<int> V;
    int status;
    int in;
    int sum = 0;
    for(int i = 0; i < n; i++){
      cin >> in;
      if(i == 0){
	if(in < 0){
	  status = -1;
	  V.push_back(in);
	}
	else if(in >= 0)status = 1;

      } else {

	if(in < 0 && status > 0){
	  status = -1;
	  V.push_back(sum);
	  sum = 0;
	  V.push_back(in);
	  continue;
	}else if(in < 0 && status < 0){
	  V.push_back(in);
	  continue;
	}else if(in >= 0 && status < 0){
	  status = 1;
	  V.push_back(sum);
	  sum = 0;
	}
      }
      sum += in;
    }
    if(sum >= 0) V.push_back(sum);

    int max = -1*N;
    
    for(int i = 0; i < V.size(); i++){
      sum = 0;
      for(int j = i; j < V.size(); j++){
	sum += V[j];
	if(max < sum) max = sum;
      }
    }
    
    cout << max << endl;
  }
  return 0;
}

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

int maximum_sub_sequense(vector<int> &s)
{
    int max = 0;

    for (auto i = s.begin(); i != s.end(); i++) {
        int sum = 0;
        for (auto j = i; j != s.end(); j++) {
            sum += *j;
            if (sum > max)
                max = sum;
        }
    }

    return max;
}

int main()
{
    for (int n; cin >> n && n;) {
        vector<int> sequence(n);
        for (int x; n-- && cin >> x;)
            sequence[n] = x;
        cout << maximum_sub_sequense(sequence) << endl;
    }

    return 0;
}

C++: /*
 * AOJ-Volume0-0022
 */
#include <iostream>
#include <limits.h>
#include <vector>
#include <cstdio>

using namespace std;

typedef long long ll;

/*
 * Find maximum sum.
 *   array: number list
 */
ll findMaximumSum(vector<int> &array){
  ll sumValue = array[0];
  ll maxValue = INT_MIN;
  int length = array.size();

  for(int i = 1; i < length; i++){
    ll value = array[i];

    if(sumValue < 0){
      sumValue = value;
    }else{
      sumValue += value;
    }

    if(maxValue < sumValue){
      maxValue = sumValue;
    }
  }

  return maxValue;
}

void solve(int n){
  vector<int> a(n);

  for(int i = 0; i < n; i++){
    scanf("%d", &a[i]);
  }

  cout << findMaximumSum(a) << endl;
}

int main(){
  int n;

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

    solve(n);
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int N = 5e3 + 11;
int a[N];

int main() {
	ios :: sync_with_stdio(false);
	int n;
	while(cin >> n) {
		if(n == 0) break;
		for(int i = 0; i < n; i ++)
			cin >> a[i];
		int ans = a[0], pre = min(a[0], 0);
		for(int i = 1; i < n; i ++) {
			a[i] += a[i - 1];
			ans = max(ans, a[i] - pre);
			pre = min(pre, a[i]);
		}
		cout << ans << '\n';
	}
	return 0;
}

C++: #include <stdio.h>

int seqsum(int n){
	int a = 0;
	int b[5000];
	int c = 0;
	int s = 0;
	int f = 0;
	scanf("%d", &a);
	b[0] = a;
	f = a;
	s = a > 0 ? 1 : -1;

	for(int i=1; i<n; ++i){
		scanf("%d", &a);
		if(s * a > 0){
			b[c] += a;
		}else{
			b[++c] = a;
			s = a > 0 ? 1 : -1;
		}
	}

	int m = b[0];
	int sum = m;
	for(int i=1; i<=c; ++i){
		if(sum < 0 && m < b[i]){
			sum = m = b[i];
		}else{
			sum += b[i];
			if(m < sum){
				m = sum;
			}
		}
	}
	return m > f ? m : f;
}

int main(void){
	int n = 0;
	while(true){
		scanf("%d", &n);
		if(n == 0){ break;}
		printf("%d\n", seqsum(n));
	}
	return 0;
}