Go: package main

import (
        "fmt"
)

func main() {
        for {
                var n int
                _, err := fmt.Scanf("%d", &n)
                if err != nil || n == 0 {
                        break
                }

                var sum [5001]int

                for i := 1; i <= n; i++ {
                        fmt.Scanf("%d", &(sum[i]))
                        if i > 1 {
                                sum[i] += sum[i-1]
                        }
                }

                max_sum := sum[1]
                for i := 1; i <= n; i++ {
                        for j := 0; j < i; j++ {
                                if max_sum < sum[i] - sum[j] {
                                        max_sum = sum[i] - sum[j]
                                }
                        }
                }
                fmt.Printf("%d\n", max_sum)
        }
}

C++: #define MINUS -99999999
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){
  int n,a[5010],j;
  long long int  sum[5010] = {0},max;
  while(1){
    max = MINUS;
    cin >> n;
    if(n == 0)break;
    for(int i = 1; i <= n; i++){
      cin >> a[i];
      sum[i] = sum[i-1] + a[i];
    }
    for(int i = 0; i < n; i++){
      for(int j = i + 1; j <= n; j++){
	if(max < sum[j] - sum[i]){
	  max = sum[j] - sum[i];
	} 
      }
    }
    printf("%lld\n",max);
  }
}

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;
}