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<stdio.h>
int main(){
	int a;
	while(1){
		scanf("%d",&a);
		if(a==0)return 0;
		long long int n[10000];
		for(int i=0;i<a;i++)scanf("%lld",n+i);
		long long int dp[10000];
		dp[0]=n[0];
		long long int max=dp[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("%lld\n",max);
	}
}

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 maxs = 0;
    int a[10001] = {0};
    REP(i,n) cin >> a[i];
    FOR(i,0,n){
      int sum =0;
      FOR(j,i,n){
        sum += a[j];
        maxs = max(sum, maxs);
      }
    }
    cout << maxs << endl;
  }
  return 0;
}

C++: #include <cstdio>

int main(){
  int n;
  while(scanf("%d",&n),n){
    int a=-100001,b=-100001,c;
    while(n--){
      scanf("%d",&c);
      if(c>0){
	if(b>0){
	  if(b+c>a){
	    a=b=b+c;
	  }else{
	    b=b+c;
	  }
	}else{
	  if(c>a){
	    a=b=c;
	  }else{
	    b=c;
	  }
	}
      }else{
	if(b<0){
	  b=c;
	}else{
	  b=b+c;
	}
      }	
    }
    printf("%d\n",a);
  }
}

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

int main(){
	int n;
	while(cin >> n, n){
		/*n++;
		vector<int> t(n), v(n);
		t[0] = 0;
		for(int i = 1;i < n;i++){
			cin >> v[i];
			t[i] = max(t[i-1]+v[i], v[i]);
		}*/
		n++;
		vector<int> vec(n),dp(n);
		for(int i=1;i<n;i++)cin >> vec[i];
		dp[0]=0;
		for(int i=1;i<n;i++)dp[i]=max(dp[i-1]+vec[i],vec[i]);
		cout << *max_element(dp.begin(), dp.end()) << endl;
	}
	return 0; 
}

/*
7
-5
-1
6
4
9
-6
-7
*/

C++: //include----------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>

//using namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

//define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define INF 1 << 25
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << "\n";
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)

//typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ull;

//const------------------------------------------------------------------------------------------------------------------------
const double EPS = 1e-10;
const double PI = acos( -1.0 );

//global-----------------------------------------------------------------------------------------------------------------------
int n;
VI ar;
LL ans = 0;
//function---------------------------------------------------------------------------------------------------------------------
void solve( int now_check, LL sum ) {
	if ( ans < sum ) {
		ans = sum;
	}
	if ( now_check == n ) {
		return;
	}
	if ( sum < 0 ) {
		return;
	}
	if ( ar[ now_check ] < 0 ) {
		solve( now_check + 1, 0 );
	}
	solve( now_check + 1, sum + ar[ now_check ] );
}

int main() {
	while ( scanf( "%d", &n ) ) {
		if ( !n ) {
			return 0;
		}
		ans = 0;
		ar.resize( n );
		rep( i, n ) {
			scanf( "%d", &ar[ i ] );
		}
		solve( 0, 0 );
		printf( "%lld\n", ans );
	}
	return 0;
}

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

int main()
{
	int n=1, count = 0;
	int a[10000],ans[10000];
	while (n != 0) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		int sum = 0, max = 0;
		for (int ai = 0; ai < n - 1; ai++) {
			for (int bi = n; bi > 0; bi--) {
				sum = 0;
				for (int k = ai; k < bi; k++) {
					sum = sum + a[k];
				}
				if (sum > max) {
					max = sum;
				}
			}
		}
		ans[count] = max;
		count++;
	}
	for (int ci = 0; ci < count-1; ci++) {
		cout << ans[ci] << endl;
	}
}

C++: #include<bits/stdc++.h>
#define ll long long
using namespace std;

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

        ll p=0;
        ll s=0;
        for(ll i=1; i<=n; i++){
            s = max(ara[i], s+ara[i]);
            p = max(p, s);
        }
        printf("%lld\n", p);
    }

    return 0;
}

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

int main() {
  int x,n;
  int a[5000];
  long int sum,nextsum;
  while(cin >> n) {
    if(n == 0) break;
    for(int i=0;i<n;i++) {
      cin >> x;
      a[i] = x;
    }
    sum = 0;
    for(int i=0;i<n;i++) {
      if(a[i] >= 0) {
        nextsum = a[i];
        for(int j=1;j<n-i;j++) {
          nextsum += a[i+j];
          if(nextsum > sum)
            sum = nextsum;
        }
      }

    }
    cout << sum << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int a;
    cin >> a;
    int sum = a;
    int max_sum = sum;
    for (int i = 1; i < n; i++) {
      cin >> a;
      sum = max(sum + a, a);
      max_sum = max(max_sum, sum);
    }
    cout << max_sum << 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] ;
		}
		
		int max , contain ;
		max = contain = 0 ;
		
		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 ;
		
	} 
	return 0 ;
 }