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

int main(){
  int n, num, a, maxx;

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

    num = maxx = 0;

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

    cout << num << endl;
  }

  return 0;
}

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

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

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

int main(){
	int n;
	cin >> n;
	while(n != 0){	

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

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

		for(int i = 0; i < n - 1 ; i++){
			sum[i][0] = a[i];
			for(int j = 0; j < n - i; j++){
				sum[i][j+1] = sum[i][j] + a[i+j+1];
				maximum = max(maximum, sum[i][j]);
			}
		}

		maximum = max(maximum, a[n-1]);
		cout << maximum << endl;
		cin >> n;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#define ll long long int
using namespace std;
typedef pair<int, int> P;

int main()
{
  ll n,a;
  ll i,j;
  ll b[5005]={};

  while(1){
priority_queue<int> pq;
    cin>>n;
    if(n==0)break;
    for(i=1;i<=n;i++){
      cin>>a;
      b[i]=b[i-1]+a;
      for(j=i;j>=0;j--)
	pq.push(b[i]-b[j]);
    }
    cout<<pq.top()<<endl;
  }
}

C++: #include <iostream>

using namespace std;

int A[50000]={0};

int main(){
  int n;

  while(cin>>n&&n>0){
    for(int i=0;i<n;++i) cin >> A[i];

    long maxsum = -1000000;
    int prefix_sum[50001]={0};
    prefix_sum[0] = 0;
    for(int i=0;i<n;++i){
      prefix_sum[i+1]=prefix_sum[i]+A[i];
    }
    for(int i=0;i<n+1;++i){
      for(int j=i;j<n+1;++j){
        if(prefix_sum[j]-prefix_sum[i]>maxsum){
          maxsum = prefix_sum[j]-prefix_sum[i];
        }
      }
    }
    cout << maxsum << endl;
  }

  return 0;
}

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

using namespace std;

#define N 2 << 20;

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;
	else if(in > 0)status = 1;
	else{
	  V.push_back(0);
	  status = 0;
	}


      } else {
	if(in == 0) {
	  V.push_back(0);
	  sum = 0;
	}

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

    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 <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
//#define scanf scanf_s

int main()
{
	int n;
	int a[5005];
	while (cin >> n, n)
	{
		memset(a, 5005, 0);
		REP(i, n) cin >> a[i];
		int s[5005];
		s[0] = 0;
		FOR(i, 1, n+1) s[i] = s[i - 1] + a[i-1];
		int ans = -INF;
		REP(i, n)
		{
			for (int j = i + 1; j < n+1; j++)
			{
				ans = max(ans, (s[j] - s[i]));
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#define INF 20000000
int array[5001];
int n,i,sum,k=0;
void tansaku(int a)
{
    k += array[a];
    if(k > sum)
    {
        sum = k;
    }
    if(a == n-1)return;
    tansaku(a+1);
}
int main(void)
{
    while(1)
    {
        for(i=0;i<5001;i++)
        {
            array[i] = 0;
        }
        scanf("%d",&n);
        if(n==0)break;
        for(i=0;i<n;i++)
        {
            scanf("%d",&array[i]);
        }
        sum = -INF;
        for(i=0;i<n;i++)
        {
            tansaku(i);
            k = 0;
        }
        printf("%d\n",sum);
    }
    return 0;
}

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

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

using namespace std;

int findMax(vector<int>& a) {
  int sum, max = 0;
  for (int i = 0; i < a.size(); i++) {
    for (int j = i, sum = 0; j < a.size(); j++) {
      sum += a[j];
      if (max < sum) max = sum;
    }
  }
  return max;
}

int main() {
  int n, t, max;
  while (1) {
    cin >> n;
    if (n == 0) break;
    vector<int> a;
    for (int i = 0; i < n; i++) {
      cin >> t;
      if (-100000 <= t && 100000 >= t)
        a.push_back(t);
    }
    cout << findMax(a) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;

#define ull unsigned long long
#define ll long long
#define rep(i,j) for(int i=0;i<j;++i)
#define loop(i,a,j) for(int i=a;i<j;++i)
#define mpi(it,table,T,E) for(map<T,E>::iterator it = table.begin();it!=table.end();++it)
#define PI 3.141592654
/*
ifstream fin("input.txt");
ofstream fout("output.txt");
#define cin fin
#define cout fout
*/
#define N 5005

int sol[N][N];
bool visited[N][N];
int data[N];

int solve(int i,int j,int sum){
	if( i == j) return sum;
	if(visited[i][j])return sol[i][j];
	
	int a = max(sum,solve(i+1,j,sum-data[i]));
	int b = max(sum,solve(i,j-1,sum-data[j]));
	sol[i][j] = max(a,b);

	visited[i][j] = true;
	return sol[i][j];
}


int main(){
	int n;
	while(scanf("%d",&n),n){
		memset(visited,0,sizeof(visited));
		memset(sol,0,sizeof(visited));
		int sum = 0;
		rep(i,n){
			scanf("%d",&data[i]);
			sum += data[i];
		}
		sum = solve(0,n-1,sum);
		printf("%d\n",sum);	
	}
	return 0;
}