Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var infty = 100000 * 2
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() (int, error) {
	scanner.Scan()
	return strconv.Atoi(scanner.Text())
}

func findMaximumSubarray(arr []int) int {
	currentMaxSum, maxSumContainsTail := -infty, -infty

	for j := range arr {
		if arr[j] > maxSumContainsTail+arr[j] {
			maxSumContainsTail = arr[j]
		} else {
			maxSumContainsTail += arr[j]
		}
		if currentMaxSum < maxSumContainsTail {
			currentMaxSum = maxSumContainsTail
		}

		// fmt.Println(arr[j], currentMaxSum, maxSumContainsTail)
	}
	return currentMaxSum
}

func main() {
	scanner.Split(bufio.ScanWords)
	for {
		size, _ := nextInt()
		if size == 0 {
			break
		}

		arr := make([]int, size)
		for i := range arr {
			n, _ := nextInt()
			arr[i] = n
		}

		maximumSum := findMaximumSubarray(arr)
		fmt.Println(maximumSum)
	}
}

C++: #include <iostream>

using namespace std;

int S[5000];

int main(){
	int n;
	while(cin >> n,n){
		S[0] = 0;
		int tmp,sum = 0;
		for(int i = 1; i <= n; i++){
			cin >> tmp;
			sum += tmp;
			S[i] = sum;
		}
		tmp = S[1] - S[0];
		for(int i = 0; i <= n; i++){
			for(int j = i + 1; j <= n; j++){
				if(tmp < S[j] - S[i]) tmp = S[j] - S[i];
			}
		}
		cout << tmp << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define INF LLONG_MAX-1
using namespace std;
typedef long long ll;

ll a[100001];
int main() {
	ll n;
	while (scanf("%lld", &n), n) {
		ll cnt = 0;
		for (ll i = 0; i < n; i++) {
			scanf("%lld", &a[i]);
			cnt += a[i];
		}
		ll Max = cnt;
		ll w = 0;
		for (ll i = 0; i < n; i++) {
			cnt -= a[i];
			if (Max < cnt) {
				w = i;
				Max = cnt;
			}
		}
		cnt = Max;
		for (ll i = n - 1; i > w; i--) {
			cnt -= a[i];
			Max = max(Max, cnt);
		}
		printf("%lld\n", Max);
	}
	return 0;
}

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 main(){
	int N;
	int x;
	while(1){cin>>N;if(N==0) break;int index=0;
		ll ans = 0;
		vl sum(N+1);
		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-2]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

int main(){
	int n,a[5000],s,ans;
	
	while(1){
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> a[i] ;
		}
			if(n==0){
				break;
			}
		ans=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<=n-i;j++){
				s=0;
				if((a[j]<=0)||(a[j+i-1]<=0)){
					goto A;
				}
				for(int k=j;k<j+i;k++){
					s+=a[k];
				}
				if(ans<s){
					ans=s;
				}
					A:;
			}
		}
		if(ans==0){
			for(int i=1;i<n;i++){
				for(int j=0;j<n-i;j++){
					if(a[j]<a[j+1]){
						s=a[j]; a[j]=a[j+1]; a[j+1]=s;
					}
				}
			}
			ans=a[0];
		}
	cout << ans << "\n" ;
	}
return 0;
}

C++: #include <cassert>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>

int solve(std::istream& in, int n)
{
    std::vector<int> all(n);
    std::vector<int> koho;
    int temp = 0;
    int max = 0;
    for(int i = 0; i < n; ++i) {
        in >> all[i];
        if(temp && all[i] < 0){
            koho.push_back(temp);
            if(temp + all[i] < 0){
                temp = 0;
            }else{
                temp += all[i];
            }
        }else
        if(all[i] > 0){
            temp += all[i];
        }
    }
    if(temp) koho.push_back(temp);
    if(koho.empty()) return *std::max_element(all.begin(), all.end());
    return *std::max_element(koho.begin(), koho.end());
}
int main()
{
    int n;
    std::cin >> n;
    while(n){
        std::cout << solve(std::cin, n) << std::endl;
        std::cin >> n;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define INF -100000000000000
int main(void){
    int n,a,max=INF;
    while(std::cin>>n){
        if(n==0) break;
        std::vector<int> dp(n+1);
        dp[0]=INF;
        for(int i=0;i<n;i++){
            std::cin>>a;
            dp[i+1]=(a>0?dp[i]+a:a);
            max=std::max(max,dp[i+1]);
        }
        std::cout<<max<<std::endl;
    
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 

int main(void){  
  int n;
  while(cin>>n,n){
    int sum = 0, maxs = 0, a;
    REP(i,n){
      cin >> a;
      if(a>=0)
        sum += a;
      else{
        maxs = max(sum, maxs);
        sum = 0;
      }
    }
    cout << maxs << endl;
  }
  return 0;
}

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

int main()
{

	int n, num;
	vector< int > a;

	while( 1 ) {

		int Max = -1000000;

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

		//??????????????????
		a.push_back( 0 );

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

		cout << Max << endl;

		a.clear();
	
	}

	return 0;

}

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

int main(){
  int n, num, a, maxx;

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

    num = 0;

    for(int i=0;i<n;i++){
      cin >> a;
      num += a;
      if(i == 0) maxx = a;
      maxx = max(maxx,num);
      if(num<0) num = 0;
    }
    cout << maxx << endl;
  }

  return 0;
}

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

C++: #include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main() {
  int n, input;
  vector<int> input_v;
  vector<int> part_v;
  int max_num = 0;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    input_v.clear();
    while(n--) {
      cin >> input;
      input_v.push_back(input);
    }
    max_num = 0;
    for (int i = 0; i < input_v.size(); ++i) {
      for (int len = 3; len <= input_v.size() - i; ++len) {
        part_v.clear();
        for (int m = 0; m < len && i + m < input_v.size(); ++m) {
          part_v.push_back(input_v[i + m]);
        }
        max_num = max(accumulate(part_v.begin(), part_v.end(), 0), max_num);
      }
    }
    cout << max_num << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int n,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=0;x<=i+1;x++){
				kazu=a[i]+a[x+1];
				if(kazu>saidai){
					saidai=kazu;
				}
			}
		}
		cout<<saidai<<endl;
	}
}