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 <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

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

#define HOGE 5000
#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define sz size()


int main(){
    int n;
    while(cin>>n){
        if(n==0)break;
        int data[HOGE];
        ll ma=-1000000000000;
        rep(i,n){
            cin>>data[i];
        }
        for(int i=0;i<n-1;i++){
            int dp[HOGE];
            dp[i]=data[i];
            for(int j=i+1;j<n;j++){
                dp[j]=dp[j-1]+data[j];
                if(dp[j]>ma)ma=dp[j];
            }
            
        }
        cout<<ma<<endl;
    }

    return 0;
}

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

int main()
{
    int n;
    while(1){
        vector<int>vec;
        int ans=0;
        cin>>n;
        vector<int>dp(n);
        if(n==0)break;
        for(int i=0;i<n;i++){
            int a;cin>>a;
            vec.push_back(a);
        }
        dp[0]=vec[0];
        for(int i=1;i<n;i++){
            dp[i]=max(dp[i-1]+vec[i],vec[i]);
        }
        for(int i=0;i<n;i++){
            ans=max(ans,dp[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        int plus[5002] = {0}, max = -100001, input;
        range(i,1,n+1){
            cin >> input;
            plus[i]+= input;
            plus[i+1] = plus[i];
        }
        rep(i,n+1){
            rep(j,i){
                if(max < plus[i] - plus[j]){
                    max = plus[i] - plus[j];
                }
            }
        }
        cout << max << endl;
    }
}

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

int a[5001];
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	while(cin >> n && n != 0){
		a[0] = 0;
		for(int i = 1; i <= n; ++i){
			cin >> a[i];
			a[i] += a[i - 1];
		}

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

		std::cout << m << std::endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 

int main(void){  
  int n;
  while(cin>>n,n){
    int maxs;
    int a[5001] = {0};
    REP(i,n) cin >> a[i];
    maxs = a[0];
    FOR(i,0,n){
      int sum =0;
      FOR(j,i,n){
        sum += a[j];
        maxs = max(sum, maxs);
      }
    }
    cout << maxs << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n;
int a[50001];
int S[50001];


int main(){

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

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

  int max = 0;
  for (int i = 0; i < n + 1; ++i)
  {
    for (int k = 0; k < i + 1; ++k)
    {
      if(S[i] - S[k] > max) max = S[i] - S[k];
    }
  }

  cout << max << endl;

  }
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <iostream>


using namespace std;

int main()
{
	int n;
	int a[5000];
	while (true)
	{
		scanf("%d", &n);
		if (n == 0)
			break;

		for (int i = 0; i < n; ++i)
			scanf("%d", a+i);

		int res = INT_MIN;
		int sum = 0;
		for (int i = 0; i < n; ++i)
		{
			sum = max(sum+a[i], 0);
			if (sum > res)
				res = sum;
		}
		printf("%d\n", res);
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <cctype>
#include <cstdlib>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a,b) (fabs((a)-(b)) < EPS)

int main(){
	int n;
	while (cin >> n && n != 0) {
		int ret = -INF;
		int a[5000];
		int memo[5000];
		for (int i = 0; i < n; ++i) cin >> a[i], memo[i] = -INF;
		for (int i = 0; i < n; ++i) {
			memo[i] = max(memo[i], a[i]);
			for (int j = i+1; j < n; ++j) {
				memo[j] = max(memo[j], memo[j-1] + a[j]);
			}
		}
		for (int i = 0; i < n; ++i) ret = max(ret, memo[i]);
		cout << ret << endl;
	}
	return 0;
}

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

int main(){
        int n,a[5010];
        while(cin >> n && n){
                int buf,j=0;
                for(int i=0; i<n; i++){
                        cin >> buf;
                        if(i>0 && a[j]*buf>=0) a[j] += buf;
                        else a[++j] = buf;
                }
                long max = 0;
                for(int i=0; i<=j; i++){
                        for(int k=i; k<=j; k++){
                                long sum = 0;
                                for(int l=i; l<=k; l++){
                                        sum += a[l];
                                }
                                max = (max > sum)? max:sum;
                        }
                }
                cout << max << endl;
        }
}

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

int main(){
  int n,m,s,a;
  // while(cin>>n,n)
  // int i,j;
  // i=1;
  // j=++i;
  // cout << i<<j;
  // i=1;
  // j=i++;
  // cout << i<<j;
  // while(++i,i<=10)
  //   cout<<i<<endl;
  while(cin>>n,n){
    cin>>a;
    m=s=a;n--;
    while(n--){
      cin>>a;
      if(s<0)s=a;
      else s+=a;
      m=max(s,m);
    }
    cout << m<<endl;
  }
  return 0;
}

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

using namespace std;
static const int MAXV = 5000;

int main() {
  while(1) {
    int n, data[MAXV], max=0, sum=0;
    scanf("%d", &n);
    if (n==0) break;
    for(int i=0; i<n; i++) scanf("%d", &data[i]);
    //cout << data[0] <<" " << data[1] <<endl;
    for (int i=0; i<n; i++) {
      for (int j=i; j<n; j++) {
	sum+=data[j];
	if(max<sum) max=sum;
      }
      sum=0;
    }
    printf("%d", max);
  }
}

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

#define REP(i,n) for(int i=0; i<n; i++)

int n;
vector<int> v;

long long s[5000][5000];

int main(){
    while(cin>>n,n){
        v=vector<int>(n);
        REP(i,n)cin>>v[i];
        long long ans=-(1<<30);
        REP(i,n){
            s[i][i]=v[i];
            ans=max(s[i][i],ans);
            for(int j=i+1; j<n; j++){
                s[i][j]=s[i][j-1]+v[j];
                ans=max(s[i][j],ans);
            }
        }
        cout<<ans<<endl;
    }
}