Go: package main

import "fmt"

func main() {
	var result []int
	for {
		var n, a, maxSum int
		fmt.Scan(&n)

		if n == 0 {
			break
		}

		maxSum = 0
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			if i == 0 {
				sum = a
				maxSum = sum
			} else if sum+a < a {
				sum = a
				if maxSum < sum {
					maxSum = sum
				}
			} else {
				sum = sum + a
				if maxSum < sum {
					maxSum = sum
				}
			}
		}

		result = append(result, maxSum)
	}

	for _, v := range result {
		fmt.Println(v)
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define pb push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
constexpr double eps = 1e-14; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int n;

constexpr long long minf = -100000 * 5000;

void solve(){
    vector<int> a(n);
    rep(i,n) cin>>a[i];

    vector<ll> sum(n+10,0);    // sum[i] = a[0] + ... + a[i-1]
    for(int i=0;i<n;i++) sum[i+1] = sum[i] + a[i]; 

    ll ans = minf-10;
    for(int i=0;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            // want a[i] + ... + a[j]
            ll k = sum[j+1] - sum[i];
            ans = max(ans,k);
        }
    }
    cout << ans << endl;
}

int main(){
    while(cin>>n){
        if(n==0) break;
        solve();
    }
}

C++: #include <cstdio>

using namespace std;

int n, a[5000];

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

        int l = 0;
        while( l < n )
        {
            int r = l + 1;
            while( r < n )
            {
                int sum = 0;
                for( int i = l; i <= r; i++ )
                {
                    sum += a[i];
                }
                if( index < sum )
                {
                    index = sum;
                }
                r++;
            }
            l++;
        }
        printf( "%d\n", index );

    }
    return 0;

}

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

int a[5001] = {0};
int main() {
  int n;
  while(cin >> n) {
  if(n == 0) break;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int maxsum = 0;
  int sum = 0;
  for(int i = 1; i <= n-1; i++){
    for(int j = i + 1; j <= n;j++) {
      for(int k = i; k <= j;k++) {
	sum += a[k];
      }
      if(sum > maxsum)
	maxsum = sum;
      sum = 0;
    }
  }
  cout << maxsum << endl;
  }
}

C++: #include <iostream>
using namespace std;
int main(void){

	int n, max=0;
	int *a;
	int **sum;

	do{
		max = 0;
		//cout << endl;
		cin >> n;

		if (n == 0) break;

		a = new int[n];
		sum = new int *[n];
		for (int i = 0; i < n; i++) sum[i] = new int[n];

		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++) sum[i][j] = 0;
		}

		for (int i = 0; i < n; i++){

			cin >> a[i];

			for (int j = i; j >= 0; j--){

				for (int k = i; k >= j; k--){
					sum[i][j] += a[k];
				}
				if (max < sum[i][j]) max = sum[i][j];
			}
		}

		cout << max << endl;
	} while (1);
}

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

inline int max(int a, int b){
	return (a>b)? a : b ;
}

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

	while( cin >> n , n ){
		for(int i=0 ; i<n ; i++){
			cin >> a[i] ;
		}
		max1 = 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++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;
int sum[3000];
int main(){
	int N;
	int ans = 0;
	while(cin>>N&&N){
		int index=0;
		ans = INT_MIN;
		FOR(i,1,N+1){
			cin>>sum[i];
			sum[i] += sum[i-1];
		}
		FOR(i,1,N+1){
			FOR(j,i,N+1){
				ans = max(ans,sum[j]-sum[i-1]);
			}
		}
		cout<< ans <<endl;
	}
}

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

C++: #include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
#define NMAX 5100

int dp[NMAX][NMAX];
int a[NMAX];

int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)break;
        memset(dp,0,sizeof(int)*NMAX);
        memset(a,0,sizeof(int)*NMAX);

        for(int i =1;i<=n;++i)
        {
            cin >> a[i];
        }
        int temp = 0,ans = 0;
        for(int first = 1;first<=n; ++first)
        {
            for(int last = first;last<=n;++last)
            {
                dp[first][last] = dp[first][last-1]+a[last];
                if(ans<dp[first][last] ) ans = dp[first][last];
            }
        }
        cout << ans << endl;
    }
    return 0;
}