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<cstdio>
#include<vector>
#include<deque>
using namespace std;
int main(){
    int n;
    for(;;){
        scanf("%d",&n);
        if(n==0) break;
        vector<int> numa(n);
        deque<int> suma;
        int ans=0;
        for(int i=0;i<n;i++){
            scanf("%d",&numa[i]);
        }
        
        bool isplus=numa[0]>=0;
        int nowsum=numa[0];
        for(int i=1;i<n;i++){
            if((numa[i]>=0) == isplus){
                nowsum+=numa[i];
            }else{
                isplus=numa[i]>=0;
                suma.push_back(nowsum);
                nowsum=numa[i];
            }
        }
        suma.push_back(nowsum);
        for(int i=0;i<suma.size();i++){
            printf("%d ",suma[i]);
        }
        printf("\n");

        if(suma.front()<0){
            if(suma.size()==1){
                int m=0;
                for(int i=0;i<n;i++){
                    if(m==0 || m<numa[i]) m=numa[i];
                }
                ans=m;
            }
            suma.pop_front();
        }else if(suma.size()==1){
            for(int i=0;i<n;i++){
                ans+=numa[i];
            }
        }

        while(!suma.empty()){
            if(suma.size()>=3){
                nowsum=suma.front();
                suma.pop_front();
                while(suma.size()>=2 && suma[0]+suma[1]>0){
                    nowsum+=suma[0]+suma[1];
                    suma.pop_front();
                    suma.pop_front();
                }
                if(suma.size()>=2) suma.pop_front();
                if(ans<nowsum) ans=nowsum;
            }else if(suma.size()==2){
                if(ans<suma[0]) ans=suma[0];
                suma.pop_front();
                suma.pop_front();
            }else if(suma.size()==1){
                if(ans<suma[0]) ans=suma[0];
                suma.pop_front();
            }
        }
        printf("%d\n",ans);
    }
}

C++: #include <iostream>
#include <climits>
typedef long long ll;
using namespace std;
 
int main(){
    int n,a[5001];
    ll ans;
    a[0]=0;
    while(cin>>n&&n){
        ans = INT_MIN;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            a[i] += a[i-1];
        }
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                ans = max(ans,a[j]-a[i-1]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main(){
	int n;

	while(cin >> n){
		int max = 0;
		int soremade = 0;
		int mae = 0;
		int ima = 0;

		if(n==0) break;
		
		cin >> mae;
		max = mae;

		for(int i=1;i<n;i++){
			if(mae > soremade+mae){
				soremade = mae;
			}else{
				soremade += mae;
			}
			
			cin >> ima;
			
			if(max < ima+soremade){
				max = ima+soremade;
			}
			mae = ima;
		}

		cout << max << endl;
	}

	return 0;
}

C++: #include <cstdio>

int main(){
  int n;
  while(scanf("%d",&n),n){
    int a=0,b=0,c;
    while(n--){
      scanf("%d",&c);
      if(c>0){
	if(b>0){
	  if(b+c>a){
	    a=b=b+c;
	  }else{
	    b=b+c;
	  }
	}else{
	  if(c>a){
	    a=b=c;
	  }else{
	    b=c;
	  }
	}
      }else{
	if(b<0){
	  b=c;
	}else{
	  b=b+c;
	}
      }	
    }
    printf("%d\n",a);
  }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int a[5001];
int sum[5001];
main(){
  while(1){
    int res=0;
    scanf("%d",&n);
    if(n==0) break;
    int sm=0;
    sum[-1]=0;
    memset(sum,0,sizeof(sum));
    for(int i=0;i<n;i++){
      scanf("%d",&a[i]);
      sm+=a[i];
      sum[i]=sm;
    }
    for(int i=0;i<n;i++)
      for(int j=i;j<n;j++)
	res=max(res,sum[j]-sum[i-1]);
    printf("%d\n",res);
  }
}

C++: #include <iostream>
using namespace std;int n,a[5000];int main(){int i,j,t,s;while(cin>>n&&n){s=0;t=-1000000;for(i=0;i<n;i++){cin >> a[i];}for(i=0;i<n;i++){s+=a[i];if(t<s)t=s;for(j=i+1;j<n;j++){s+=a[j];if(t<s)t=s;}s=0;}cout<<t<<endl;}return 0;}

C++: #include<stdio.h>
int main(){
	int a;
	while(1){
		scanf("%d",&a);
		if(a==0)return 0;
		long long int n[10000];
		for(int i=0;i<a;i++)scanf("%lld",n+i);
		long long int dp[10000];
		dp[0]=n[0];
		long long int max=dp[0];
		for(int i=1;i<a;i++){
			if(dp[i-1]>0)dp[i]=dp[i-1]+n[i];
			else dp[i]=n[i];
			if(max<dp[i])max=dp[i];
		}
		printf("%lld\n",max);
	}
}

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 = 0;
    int a[10001] = {0};
    REP(i,n) cin >> a[i];
    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 <cstdio>

int main(){
  int n;
  while(scanf("%d",&n),n){
    int a=-100001,b=-100001,c;
    while(n--){
      scanf("%d",&c);
      if(c>0){
	if(b>0){
	  if(b+c>a){
	    a=b=b+c;
	  }else{
	    b=b+c;
	  }
	}else{
	  if(c>a){
	    a=b=c;
	  }else{
	    b=c;
	  }
	}
      }else{
	if(b<0){
	  b=c;
	}else{
	  b=b+c;
	}
      }	
    }
    printf("%d\n",a);
  }
}

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

int main(){
	int n;
	while(cin >> n, n){
		/*n++;
		vector<int> t(n), v(n);
		t[0] = 0;
		for(int i = 1;i < n;i++){
			cin >> v[i];
			t[i] = max(t[i-1]+v[i], v[i]);
		}*/
		n++;
		vector<int> vec(n),dp(n);
		for(int i=1;i<n;i++)cin >> vec[i];
		dp[0]=0;
		for(int i=1;i<n;i++)dp[i]=max(dp[i-1]+vec[i],vec[i]);
		cout << *max_element(dp.begin(), dp.end()) << endl;
	}
	return 0; 
}

/*
7
-5
-1
6
4
9
-6
-7
*/