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++: #include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
int n,a[5008];
int b[5008];

int solve(){
  int res= -100001;
  for(int i=0;i<=n;i++){
    for(int j=n;j>i;j--){
      res=max(res,b[j]-b[i]);
    }
  }
  return res;
}

int main(){
  while(1){
    scanf("%d",&n);
    if(n==0) break;
    for(int i=0;i<n;i++){
      scanf("%d",&a[i+1]);
    }

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

    printf("%d\n",solve());

  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<cfloat>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<climits>
#include<cstring>
#include<queue>
#include<map>
#include<functional>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
const int mod = 1e9+7;
const int INFINT = INT_MAX/2;
typedef long long ll;
typedef unsigned long long ull;

int main(){
    int n;
    cin >> n;
    while(n!=0){
        int a[n];
        REP(i, n){
            cin >> a[i];
        }
        int maxSum = a[0];
        int res = -INFINT;
        for (int i=1;i<n;i++){
            if(maxSum + a[i] < a[i]){
                maxSum = a[i];
            }else{
                maxSum += a[i];
            }
            res = max(res, maxSum);
        }
        cout << res << endl;
        cin >> n;
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<functional>
#include<queue>
#include<stack>
#include<math.h>
#define INF 999999
#define EPS 1.0e-10

using namespace std;

typedef long long ll;

int n;

int main()
{
	while (cin >> n) {
		if (n == 0)return 0;
		ll d[5250];
		
		d[0] = 0;
		int a;
		for (int i = 1; i <= n; i++) {
			cin >> a;
			d[i] = d[i - 1] + a;
		}
		ll max = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < i; j++) {
				if (d[i] - d[j] > max)max = d[i] - d[j];
			}
		}
		cout << max << endl;
	}

}

C++: #include<iostream>
using namespace std;
int main() {
	int n;
	int a[5000];
	while (cin >> n&&n != 0) {
		int sum3 = -100000;
		for (int i = 0; i<n; i++)
			cin >> a[i];
		for (int i = 0; i<n; i++) {
			int sum = -10001;
			int sum2 = -10001;
			if(a[i] < 0) goto yo;
			for (int j = 0; j <= i; j++) {
				if (a[j]<0||(j<=1&&a[j-1]<0&&(a[j-1]+a[j])<0)) goto stop;;
				sum2 = 0;
				for (int z = j; z <= i; z++) {
					sum2 += a[z];
					if (sum < sum2) {
						sum = sum2; 
					}
				}
			stop:;
			}
		yo:;
			if (sum < a[i]) sum = a[i];
			if (sum3<sum) sum3 = sum;
		}
		cout << sum3 << endl;
	}
}

C++: 


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b)          for(i=a;i<b;i++)
#define repr(i,a,b)         for(i=a;i>=b;i--)

#define LL                  long long
#define ull                 unsigned long long int
#define scl(n)              scanf("%lld", &n)
#define scf(n)              scanf("%lf", &n)
#define sci(n)              scanf("%d", &n)
#define scii(n,m)           scanf("%d %d",&n,&m)
#define sciii(n,m,p)        scanf("%d %d %d",&n,&m,&p)
#define scs(n)              scanf("%s", n)
#define pfi(n)              printf("%d\n",n)
#define pfl(n)              printf("%I64d ",n)
#define pfs(s)              printf("%s\n",s)

#define MOD                 1000000007
#define MODP                99999999999973LL
#define pb                  push_back
#define ff                  first
#define ss                  second
#define mp                  make_pair
#define pp                  pair<int,int>
#define memo(a,b)           memset(a,b,sizeof(a))
#define INF                 1e9
#define EPS                 1e-8
#define Pi                  acos(-1.0)

#define N 100005


int main()
{

    int n;


    while(cin>>n && n)
    {

        LL arr[5000];

        for(int i=0;i<n;i++)
            scl(arr[i]);


        LL sum=0;
        LL res=LONG_LONG_MIN;


        for(int i=0;i<n;i++)
        {
            sum+=arr[i];
            
            res=max(res,arr[i]);
            res=max(res,sum);

            if(sum<=0)
                sum=0;
        }
       // cout<<"sss"<<endl;
       cout<<res<<endl;

    }

    return 0;
}

/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction

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=in[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++: #include <cstdio>
#include <bitset>
#include <cmath>
#include <complex>
#include <vector>
#include <algorithm>
#include <stack>
#include <iostream>
#define EPS 1e-6

using namespace std;
typedef complex<double> coor;

double cross(coor a, coor b)
{
  return a.real() * b.imag() - a.imag() * b.real();
}

double dot(coor a, coor b)
{
  return a.real() * b.real() + a.imag() * b.imag();
}

int main()
{
  int N;
  while (scanf(" %d ", &N) == 1) {
    if (N == 0) {break;}
    vector<int> series(N);
    
    for (int i = 0; i < N; i++) {
      int n;
      scanf(" %d ", &n);
      series[i] = n;
    }
    
    int mx = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
	int sum = 0;
	for (int k = i; k <= j; k++) {
	  sum += series[k];
	}
	//printf("sum = %d\n", sum);
	mx = max(mx, sum);
      }
    }
    printf("%d\n", mx);
  }
  return 0;
}

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

int main()
{

	int n, num;
	vector< long int > a;
	vector< long int > res;

	while( 1 ) {

		int Max = -100001;

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

		for( int i = 0; i < n; i++ ) {
		
			cin >> num;
			a.push_back( num );
			
		}

		//?????§???
		for( int i = 1; i < n; i++ ) {
		
			a[i - 1] + a[i] > a[i] ? a[i] = a[i - 1] + a[i] : a[i] = a[i];

			if ( a[i] > Max ) {
				Max = a[i];
			}
		
		}

		res.push_back( Max );
		a.clear();
	
	}

	for( vector< long int > :: iterator mIt = res.begin(); mIt != res.end(); ++mIt ) {
		cout << *mIt << endl;
	}

	return 0;

}

C++: # Kadane
module Kadane
  def self.max_subarray(a)
    max_so_far =  max_ending_here = 0
    a.each do |i|
      max_ending_here = [0, max_ending_here + i].max
      max_so_far = [max_so_far, max_ending_here].max
    end
    max_so_far
  end
end

answers = []

while n = gets.to_i
  break if n == 0
  a = []
  n.times { a << gets.to_i }
  answers << Kadane.max_subarray(a)
end

puts answers