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>
using namespace std;
int main(){
    int n=0,a[5000]={0},saidai=-100000;
    while(cin>>n&&n!=0){
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;n>i;i++){
            int kazu=0;
            for(int x=i;x<n;x++){
                kazu=kazu+a[x];
                if(kazu>saidai){
                    saidai=kazu;
                }
            }
        }
        cout<<saidai<<endl;
    }
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

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

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
ll a[5000];


int main(){
	while(cin>>n,n!=0){
		int c=0;
		ll MAX=-10000000;
		cin>>a[0];
		rep(i,n-1){
			int t;
			cin>>t;
			if(a[c]*t<0){
				a[++c]=t;
			}
			else a[c]+=t;
		}
		
		rep(i,c+1){
			ll max1=0,max2=0;
			ll t=0;
			for(int j=i-1;j>-1;j--){
				t+=a[j];
				if(max1<t){
					max1=t;
				}
			}
			t=0;
			for(int j=i+1;j<c+1;j++){
				t+=a[j];
				if(max2<t){
					max2=t;
				}
			}
			//cout<<max1<<" "<<max2<<endl;
			if(MAX<a[i]+max1+max2) MAX=a[i]+max1+max2;
			//cout<<endl;
		}
		cout<<MAX<<endl;
		
		//rep(i,c+1) cout<<a[i]<<endl;
		//cout<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
int n;
int b;
int c;
int d;
int e;
while(cin >>n){
if(n==0){break;}
int a[n];
b=0;
while(b<n){
cin >>a[b];
b=b+1;}
d=a[0];
b=0;
while(b<n){
e=0;
c=b;
while(c<n){
e=e+a[c];
if(e>d){d=e;}
c=c+1;}
b=b+1;}
cout <<d<<endl;}}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[6000],n,a,i,maxm;
int main(){
  while(1){
    cin>>n;
    maxm=0;
    if(n==0)break;
    cin>>a;
    dp[1]=maxm=a;
    for(int i=2;i<=n;i++){
      cin>>a;
      dp[i]=dp[i-1]+a;
      if(dp[i]<0)dp[i]=0;
      maxm=max(dp[i],maxm);
    }
    cout<<maxm<<endl;
  }
  return 0;
}

C++: #include<stdio.h>

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0){return 0;}
		
		if( n >= 1 && n <= 5000){
		int a[5000]={0},max=0;
		for(int x=0;x<n;x++)
		{
			scanf("%d",&a[x]);

			for(int y=0;y<=x;y++)
			{
				if(y!=x)
				{
					a[y]+=a[x];
				}
				if(max<a[y])
				{
					max=a[y];
				}
			}
		}
		printf("%d\n",max);
		}
	}
}

C++: #include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
	int g,t=-192839,n,a[109000];
	while(1)
	{
	cin>>n;
	if(n==0)
		break;
	for(int i=0;i<n;i++)
		cin>>a[i];
	t=-192839;
	for(int i=0;i<n;i++)
	{
		g=0;
		for(int j=i;j<n;j++)
		{
			g+=a[j];
			t=max(g,t);
		}
	}
	cout<<t<<endl;	
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    int N;
    while (cin >> N, N) {
        ll res = -114514;
        bool ok = 0;
        vector<ll> a(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
            res = max(res, a[i]);
        }

        if (res > 0) ok = 1;
        
        ll sum = 0;
        for (int i = 0; i < N; i++) {
            sum += a[i];
            if (sum < 0) sum = 0;
            if (ok) {
                res = max(res, sum);
            }
        }
        cout << res << endl;
    }
    return 0;
}

C++: #include<stdio.h>
int main(){
    int a;
    while(1){
        scanf("%d",&a);
        if(a==0)return 0;
        int n[5000];
        for(int i=0;i<a;i++)scanf("%d",n+i);
        int dp[5000];
        dp[0]=n[0];
        int max=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("%d\n",max);
    }
}

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

using namespace std;

int main(){

  int n;


  while(cin >> n){
    if(n == 0) break;
    vector<int> V;
    int status = 1;
    int in;
    int sum = 0;
    for(int i = 0; i < n; i++){
      cin >> in;
      if(i == 0){
	if(in < 0) status = -1;
	else status = 1;

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

    V.push_back(sum);

    double max = -50000000000;
    
    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>
int n[5000];
int main(){
	while(1){
		int a;
		std::cin >> a;
		if(a==0)return 0;
		int dp[5000];
		for(int i = 0; i < a; ++i){
			std::cin >> n[i];
		}
		int max = 0;
		dp[0]=n[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];
		}
		std::cout<<max<<std::endl;
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  while( cin >> n ,n)
    {
      int a[1001] = {0};
      for(int i = 0 ; i < n ; i++ )
	{
	  cin >> a[i];
	}

      int dp[5001] = {0},mx = 0;

      dp[0] = a[0];
      for(int i = 1 ; i <= n ; i++ ){
	dp[i] = max(dp[i-1] + a[i],a[i]); 
	mx = max(dp[i],mx);
      }
      cout << mx << endl;
    }
  return 0;
}

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

int main(){
	int n, max1, max2, a[5001];

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

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

C++: //O(n^2)
#include<stdio.h>
#include<math.h>

long long n,rui[10000];

long long max( long long x,long long y ){
	if( x > y )
		return x;
	return y;
}

int main()
{
	long long i,j;
	long long ans;
	while( scanf("%lld",&n) != EOF )
	{
		if( n == 0 )break;
		ans = 0;
		for( i = 0;i < n;i++ ){
			scanf("%lld",rui+i );
			if( i )rui[i] += rui[i-1];
		}
		
		for( i = 0;i < n;i++ ){
			for( j = i;j < n;j++ ){
				if( i )
					ans = max( ans, rui[j]-rui[i-1] );
				else
					ans = max( ans,rui[j] );
			}
		}
		
		printf("%lld\n",ans);
	}
	return 0;
}

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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

int main() {
  ll n, a;
  while (true) {
    cin >> n;
    if (n == 0) return 0;
    vector<ll> dp(n, -INFL);
    cin >> dp[0];  // n番目に切った時の最大値
    for (int i = 0; i < n - 1; i++) {
      cin >> a;
      dp[i + 1] = max(dp[i] + a, a);
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
  }
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <map>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define PB push_back
#define MP make_pair
int main(){
  int n;
  while(cin>>n){
    if(n==0)break;
    int a[5000],sum[5001];
    sum[0]=0;
    for(int i=0;i<n;i++){
      cin>>a[i];
      sum[i+1]=sum[i]+a[i];
    }
    int ans=0;
    for(int i=0;i<n;i++){
      for(int j=i+1;j<=n;j++){
        ans=max(ans,sum[j]-sum[i]);
      }
    }
    cout<<ans<<endl;
  }
}