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++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#define REP(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define ALL(a) begin(a),end(a)
#define ifnot(a) if(not (a))
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else 
const bool test = false;
#endif

int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
const int MAX = (int)2e5 + 5;

int main() {
	int n;
	while (1) {
		cin >> n;
		if (n == 0) break;
		vector<int> a(n);
		rep(i, n) cin >> a[i];
		ll best = INT_MIN;
		rep(i, n) {
			ll j_best = 0;
			REP(j, i, n) {
				j_best += a[j];
				if (j_best > best) best = j_best;
			}
		}
		cout << best << endl;
	}
	return 0;
}

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 = -700000000;
    
    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>
#include <algorithm>
#include <cmath>
using namespace std;


int main(){
	int n;
	int ary[5010];
	int sum[5020] = { 0 };

	while (cin >> n, n != 0){
		int max_ = numeric_limits<int>::min();
		for (int i = 0; i < n; i++){
			cin >> ary[i];
		}
		for (int i = 1; i < n+1; i++){
			sum[i] = sum[i - 1] + ary[i-1];
		}
		for (int i = 1; i < n + 1; i++){
			for (int j = i; j < n + 1; j++){
				max_ = max(max_, sum[j] - sum[i - 1]);
			}
		}
		cout << max_ << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<utility>
#include<vector>
using namespace std;
int main() {
	int n, a[5001], rui[5002] = { 0 };
	while (1) {
		cin >> n;
		if (n == 0)break;
		for (int i = 0; i < n; ++i) {
			cin >> a[i];
			rui[i + 1] = rui[i] + a[i];
		}
		int max = -100001;
		for (int i = 0; i <= n; ++i) {
			for (int j = i; j <= n; ++j) {
				if (rui[j] - rui[i] > max) {
					max = rui[j] - rui[i];
				}
			}
		}
		cout << max << endl;
	}
	return 0;
}

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

int main(void){
	int n;
	int i;
	int temp;
	int sum[5000];
	int num;
	
	while(1){
		for(i=0; i<5000; i++){
			sum[i] = 0;
		}
		num = 0;
	
		scanf("%d", &n);
		if(n == 0) break;
		
		for(i=0; i<n; i++){
			scanf("%d", &temp);
			if(temp > 0 && sum[num] < 0){
				num++;
				sum[num] = temp;
			}else if(temp >= 0){
				sum[num] += temp;
			}else{
				num++;
				sum[num] = sum[num-1] + temp;
			}	
		}

		sort(sum, sum + 5000, greater<int>());
		
		printf("%d\n", sum[0]);
		
	}
	
	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;
int a[5010];
int dp[5010];
int main(){
	while(cin>>n,n){
		for(int i =0;i < n;i++){
			cin>>a[i];
			dp[i+1]=a[i];
		}
		dp[0] = 0;
		int ans = 0;
		for(int i =1;i <= n;i++){
			dp[i] =max(dp[i-1]+a[i-1],dp[i]);
			ans =max(ans,dp[i]);
		}
		cout <<ans<<endl;
	}
	return 0;
}

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

vector<int> num;

int main(void){
  while(true){
    int maximum = 0, n;
    cin >> n;
    if(!n)  break;
    num.clear();
    for(int i = 0; i < n; i++){
      int tmp;
      cin >> tmp;
      num.push_back(tmp);
    }
    for(int i = 0; i < num.size(); i++){
      int sum = num[i];
      for(int j = i + 1; j < num.size(); j++){
        sum += num[j];
        if(sum > maximum) maximum = sum;
      }
      if(sum > maximum) maximum = sum;
    }
    cout << maximum << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int n,a[5000],saidai=0;
	while(cin>>n&&n!=0){
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		for(int i=0;n<i;i++){
			for(int x=n;x>=i;x--){
				int kazu=0;
				for(int y=x;y>=i;y--){
					kazu=kazu+a[y];
				}
				if(kazu>saidai){
					saidai=kazu;
				}
			}
		}
		cout<<saidai<<endl;
	}
	
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int num[5001];
/*
int sum(int a,int b){
  int s=0;
  for(int i=0;i<b;i++){
    s+=num[a+i];
  }
  return s;
}
*/
int main(){
  int max;
  int n,sum;
  
  while(cin>>n,n){
    max=-10000000;
    // int sum=-10000000;
    for(int i=0;i<n;i++){
      cin>>num[i];
    }
    for(int i=0;i<n-1;i++){
      sum=0;
      for(int j=i;j<n;j++){
	sum+=num[j];
	if(max<sum)max=sum;
      }
    }
    cout<<max<<endl;
  }
}

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

bool state = false ;

int main()
{
	int n ;
	while(1)
	{
		cin >> n ;
		if( ! n ) return 0 ;
		
		int p[5005];
		
		for(int i = 0 ; i < n ; i ++)
		{
			cin >> p[i] ;
		}
		
		long long max , contain ;
		contain = 0 ;
		max = 0x3f3f3f3f ;
		max *= -1 ;
		
		for(int i = 0 ; i < n ; i ++)
		{
			if(contain < 0) contain = 0 ;
			 
			contain += p[i] ;
			
			if(contain > max) max = contain ;
		}
		
	//	if(state) cout << endl ;
		
	//	state = true ;
	
		cout << max << endl ;
		
	} 
	return 0 ;
 }

C++: #include <iostream>

using namespace std;

int n, a[5010], memo[5010];

void solve(){
  memo[0] = max(0, a[0]);
  int ans = 0;
  for(int i = 1 ; i < n ; i++){
    memo[i] = max(memo[i], memo[i-1]+a[i]);
    ans = max(memo[i], ans);
  }
  cout << ans << endl;
}
int main(){

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

C++: #include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;

int main() {
	while(true) {
		int n; scanf("%d", &n);
		if(n==0) break;
		int submax; scanf("%d", &submax);
		int maxval = submax;
		for(int i = 1; i < n; i++) {
			int a; scanf("%d", &a);
			submax = max(a, submax+a);
			maxval = max(maxval, submax);
		}
		printf("%d\n", maxval);
	}
	return 0;
}