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<algorithm>  
#include<cstdio>  
#include<cstring>  
#include <deque> 
using namespace std;  

bool state = false ;

int main()
{
	int n ;
	while(1)
	{
		cin >> n ;
		if( ! n ) return 0 ;
		
		int p[5005];
		
		for(int i = 0 ; i < n ; i ++)
		{
			cin >> p[i] ;
		}
		
		long long max , contain ;
		contain = 0 ;
		max = 0x3f3f3f3f ;
		max *= -1 ;
		
		for(int i = 0 ; i < n ; i ++)
		{
			if(contain < 0) contain = 0 ;
			 
			contain += p[i] ;
			
			if(contain > max) max = contain ;
		}
		
	//	if(state) cout << endl ;
		
	//	state = true ;
	
		cout << max << endl ;
		
	} 
	return 0 ;
 }

C++: #include <iostream>

using namespace std;

int n, a[5010], memo[5010];

void solve(){
  memo[0] = max(0, a[0]);
  int ans = 0;
  for(int i = 1 ; i < n ; i++){
    memo[i] = max(memo[i], memo[i-1]+a[i]);
    ans = max(memo[i], ans);
  }
  cout << ans << endl;
}
int main(){

  while(cin >> n, n){
    fill(memo, memo+n, 0);
    for(int i = 0 ; i < n ; i++){
      cin >> a[i];
    }
    solve();
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;

int main() {
	while(true) {
		int n; scanf("%d", &n);
		if(n==0) break;
		int submax; scanf("%d", &submax);
		int maxval = submax;
		for(int i = 1; i < n; i++) {
			int a; scanf("%d", &a);
			submax = max(a, submax+a);
			maxval = max(maxval, submax);
		}
		printf("%d\n", maxval);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int n,a[5000];
	while(1){
		int maxa=-10001;
		scanf("%d",&n);
		if(n==0)
			break;
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		for(int i=0;i<n;i++){
			int sum=0;
			for(int j=i;j<n;j++){
				sum+=a[j];
				maxa=max(maxa,sum);
			}
		}
		printf("%d\n",maxa);
	}
	return 0;
}

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

typedef long long ll;

ll a[5001];

int main() {
  int num;
  while( cin >> num, num ) {
    ll maxn = 0;
    for ( int i = 0; i < num; i++ ) cin >> a[i];
    for ( int i = 0; i < num; i++ ) {
      ll sum = 0;
      for ( int j = i; j < num; j++ ) {
	sum += a[j];
	maxn = max(maxn,sum);
      }
    }
    cout << maxn << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    int dp[n][n];
    vector<int>a(n);
    int ans=-2000000000;
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++){
      cin>>a[i];
      for(int j=0;j<=i;j++){
	if(j!=i){
	  dp[j][i]+=a[i]+dp[j][i-1];
	}else{
	  dp[j][i]=a[i];
	}
	ans=max(ans,dp[j][i]);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	int 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++: #include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#include <numeric>
#include <ctype.h>
#include <cstdlib>
#include <string.h>
#include <cmath>
#include <cstdio>





using namespace std;

int main()
{
	int n;

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

		int sum = 0, input, maxer = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> input;
			sum += input;

			if (sum < 0)
				sum = 0;
			maxer = max(sum, maxer);
		}

		cout  <<  maxer << endl;
	}


	return 0;
}

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

int main(){
	int i, j, k, n, sum, result, head, head_before;
	int a[5000];
	while(cin >> n){
		if(n == 0){
			break;
		}
		result = 0;
		head = 0;
		head_before = 0;
		for (i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (i = 0; i < n; i++) {
			if(head > head_before){
				continue;
			}
			for (j = 1; j <= n-i; j++) {
				sum = 0;
				for (k = 0; k < j; k++) {
					sum += a[i+k];
				}
				if(result < sum){
					result = sum;
				}
			}
			head_before = head;
			head += a[i];
		}
		cout << result << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <deque>
#include <numeric>
#include <vector>

using namespace std;

template <typename C>
vector<int> compact(const C& container) {
  vector<int> compacten;
  int sign = 1;
  int acc = 0;
  for (auto n : container) {
    if (sign * n >= 0) {
      acc += n;
    } else {
      compacten.push_back(acc);
      acc = n;
      sign *= -1;
    }
  }
  compacten.push_back(acc);
  return compacten;
}

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

    deque<int> seq;
    for (int i = 0; i < n; ++i) {
      int x;
      scanf("%d", &x);
      seq.push_back(x);
    }
    while (seq.size() != 0 && seq.front() <= 0) { seq.pop_front(); }
    while (seq.size() != 0 && seq.back() <= 0) { seq.pop_back(); }

    auto compacten_seq = compact(seq);
    vector<int> partial_sums = { compacten_seq.back() };
    for (int i = compacten_seq.size() - 1; i > 0; i -= 2) {
      int s = partial_sums.back() + compacten_seq[i - 1];
      partial_sums.push_back(compacten_seq[i - 2] + (s < 0 ? 0 : s));
    }
    int max_sum = accumulate(
        partial_sums.begin(),
        partial_sums.end(),
        partial_sums.front(),
        [](int a, int b) { return max(a, b); });
    printf("%d\n", max_sum);
  }

  return 0;
}

C++: /* AOJ 0022*/

#include<iostream>
#include<queue>
using namespace std;

long int m;
int n;

void calc(int *data,int num,int sum){
	if(sum>=m){
		m=sum;
	}
	if(num==n){
		return;
	}
	else{
		calc(data,num+1,sum+data[num]);
	}
}

int main(){

	queue<int> ans;

	while(cin>>n){
		if(n==0){
			break;
		}
		int *data = new int[n];
		m=0;
		for(int i=0;i<n;i++){
			cin>>data[i];
		}

		for(int i=0;i<n;i++){
			calc(data,i,0);
		}
		ans.push(m);
	}

	int size=ans.size();
	for(int i=0;i<size;i++){
		cout<<ans.front()<<endl;
		ans.pop();
	}

	return 0;
}