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 <vector>

using namespace std;

int main(int argc, char *argv[]) {
  vector<int> maxsums, a;
  int n, tmp;

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      a.push_back(tmp);
    }

    for (int i = 0; i < a.size(); i++) {
      tmp = a[i];
      for (int j = i + 1; j < a.size(); j++) {
        tmp += a[j];
        if (tmp > a[i])
          a[i] = tmp;
      }
    }

    int max = a[0];
    for (int i = 0; i + 1 < a.size(); i++)
      if (max < a[i])
        max = a[i];
    maxsums.push_back(max);
    a.clear();
  }

  for (int i = 0; i < maxsums.size(); i++)
    cout << maxsums[i] << endl;
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template<class T> using vv=vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int n;

int calc(int s, int a[], int b[]){
    if(b[s] != 0) {
        return b[s];
    }

    if(s == n-1) {
        return b[n-1] = max(0, a[n-1]);
    }
    return b[s] = max(a[s]+calc(s+1,a,b), 0);
}

int main() {
    while(1) {
        int a[5000] = {};
        int b[5000] = {};
        bool f = false;
        int max = -100000;
        cin >> n;
        if(n == 0) {
            break;
        }
        rep(i,n) {
            cin >> a[i];
            if(a[i] >= 0 && f == false) {
                f = true;
            }
        }

        if(f == false) {
            rep(i,n){
                if(a[i] > max) {
                    max = a[i];
                }
            }
            cout << max << endl;
            continue;  
        }

        calc(0,a,b);
        FOR(i,0,n) {
            if(b[i] > max) {
                max = b[i];
            }
        }
        cout << max << endl;
    }
	return 0;
}

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

using namespace std;
typedef long long ll;

#define PI 3.141592653

int main(){
    int n;
    
    while(1){
        int sum[5002],ans=-200000000,a;
        scanf("%d",&n);
        
        if(n==0) break;
        
        sum[0]=0;
        sum[1]=0;
        
        for(int i=1;i<=n;i++){
            scanf("%d",&a);
            
            sum[i]+=a;
            sum[i+1]=sum[i];
            
            for(int j=0;j<i;j++){
                ans=max(ans,sum[i]-sum[j]);
            }
        }
        
        printf("%d\n",ans);
    }
    
    return 0;
}

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

int main(){
	
	int n;
	int in[6000],ans;
	
	while(cin>>n,n){
		for(int i=0;i<n;i++){
			cin>>in[i];
		}
		ans=0;
		for(int i=0;i<n;i++){
			int sum=0;
			for(int j=i;j<n;j++){
				sum=sum+in[j];
				ans=max(sum,ans);
			}
		}
		
		cout<<ans<<endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>

typedef long long ll;
using namespace std;


int sum(int i,int* bit){
	int s = 0;
	while(i > 0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int i, int x,int* bit,int n){
	while(i <= n){
		bit[i] += x;
		i += i & -i;
	}	
}

int main()
{
	int T;
	while(~scanf("%d",&T)){
		if(T==0) break;
		int* bit = new int[T+1]();	
		int res=numeric_limits<int>::min();
		for(int t=1;t<=T;t++){
			int tmp;
			scanf("%d",&tmp);
			add(t,tmp,bit,T);
		}

		for(int a=0;a<=T;a++){
			for(int b=a;b<=T;b++){
				res=max(sum(b,bit) - sum(a,bit),res);
			}
			//cout << sum(a,bit) << endl;
		}
		
		printf("%d\n",res);
	}
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(void){
    for(;;){
        int n;
        int a[10000]={0};
        cin>>n;
        if(n==0)break;
        int sum=0;
        int ans=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;i<n;i++){
            sum=0;
            for(int j=i;j<n;j++){
                sum+=a[j];
                ans=max(ans,sum);
            }
        }
        cout<<ans<<endl;

    }


	return 0;
}

C++: #include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000001
#define IN_MAX -9223372036854775808

using namespace std;
#define ll long long
#define dbg if(0)



int main(){
    while(true){
        int n;
        ll ans= IN_MAX;
        cin>>n;
        if (n==0)break;

        ll rui[n+1];
        rui[0]=0;
        for(int i=1;i<=n;i++){
            cin >>rui[i];
            rui[i]+=rui[i-1];
            dbg cout <<rui[i]<<" ";
        }
        //次を見る
        //今までの最小値を更新したか？
        //したなら暫定解を確定しansと比較。最大値=最小値=発見した最小値にする
        //してなければ最大値の更新をする
        //見る場所が一番後ろまで行ったら終了。
        ll nmin=rui[0],nmax=rui[0];
        for(int i=0;i<=n;i++){
            if (rui[i]<nmin){
                ans=max(nmax-nmin,ans);
                nmax=rui[i];
                nmin=rui[i];
            }else{
                if(rui[i]>nmax){
                    nmax=rui[i];
                }
            }
            dbg cout <<nmax<<" "<<nmin<<endl;
        }
        ans=max(nmax-nmin,ans);
        dbg cout << endl;
        cout<<ans<<endl;
    }

    return 0;
}

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

static const int MIN = -1 * (1 << 21);

int main() {
	int i, j, n, a, maxa, max_so_far, max_ending_here;
	
	while (1) {
		cin >> n;
		if (n == 0)break;
		max_so_far = 0;
		max_ending_here = 0;
		maxa = MIN;
		for (i = 0; i < n; i++) {
			cin >> a;
			maxa = max(maxa, a);
			max_ending_here = max_ending_here + a;
			if (max_ending_here < 0)max_ending_here = 0;
			if (max_so_far < max_ending_here)max_so_far = max_ending_here;
		}
		if (max_so_far == 0)max_so_far = maxa;
		cout << max_so_far << endl;

	}

	return 0;
}

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

using namespace std;

int main(void)
{
    ios::sync_with_stdio(false);
    while(1) {
        int n;
        int a[5001] = {0};
        long int ans = -10000000;
        cin >> n;
        if(n == 0) break;

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

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

    return 0;
}