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 main(){
    int n;
    while(cin>>n){
        if(!n) break;
        int a[n];
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        int mx=-99999999;
        for(int i=0; i<n; i++){
            int sum=0;
            for(int j=i; j<n; j++){
                if(mx < sum+a[j]) mx = sum+a[j];
                sum += a[j];
            }

        }
        cout << mx << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;





int main()
{
	int n;
	long long a[5000];
	long long b[5000];//i???????????§??????
	long long m, M;
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n; i++) {
			if (i == 0) b[i] = a[i];
			else b[i] = b[i - 1] + a[i];
		}
		for (int i = 0; i < n; i++) {
			if (i == 0) {
				m = min({ (long long)0,b[0] });
				M = max({ (long long)0,b[0] });
			}
			else {
				if (b[i] - m > M) {
					M = b[i] - m;
				}
				if (b[i] < m) {
					m = b[i];
				}
			}
		}
		cout << M << endl;
	}
		





}

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

int main()
{
	int n;
	while (cin >> n&&n > 0) {
		int a, maxsum = 0;
		for (int i = 0;i < n;++i) {
			cin >> a;
			if (a > 0) {
				maxsum += a;
			}
		}
		cout << maxsum << endl;
	}
}

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

void solve(int n) 
{
    int seq[n];
    for (int i = 0; i < n; i++) {
        cin >> seq[i];
    }

    bool all_minus = true;
    for (int i = 0; i < n; i++) {
        if (seq[i] >= 0) {
            all_minus = false;
        }
    }
    if (all_minus) {
        int min = seq[0];
        for (int i = 1; i < n; i++) {
            min = seq[i] > min ? seq[i] : min;
        }
        cout << min << endl;
        return;
    }

    int cur_sum = seq[0];
    int cur_max = seq[0];

    for (int i = 1; i < n; i++) {
        cur_sum += seq[i];
        if (cur_sum < 0) {
            cur_sum = 0;
        } else {
            cur_max = cur_sum > cur_max ? cur_sum : cur_max;
        }
    }
    cur_max = (cur_max > cur_sum ? cur_max : cur_sum);
    cout << cur_max << endl;
}

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

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

int main(){
  int n;
  int a[5000];
  while(cin >> n && n>0){
    for(int i=0;i<n;i++)
      cin >> a[i];
    long long sum=0,tmp=0,ma=-100000;
    int m=-100000;
    bool flg=true;
    for(int i=0;i<n;i++){
      if(flg) m=max(m,a[i]);
      if(a[i]>0){
	flg=false;
	sum+=a[i];
	continue;
      }
      ma=ma>sum?ma:sum;
      for(tmp=sum;i<n && a[i]<=0;i++){
	tmp+=a[i];
      }
      if(tmp>0)
	sum=tmp;
      else
	sum=0;
    }
    ma=ma>sum?ma:sum;

    cout << (flg?m:ma) << 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 main(){
	int N;
	int x;
	while(cin>>N&&N){
		int index=0;
		int ans = 0;
		vi 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 <iostream>
#include <cstring>
using namespace std;

int main(){
    int n, ans, sum;
    int a[5001];
    
    while(cin >> n){
        if(n == 0){
            break;
        }
        
        memset(a, 0, sizeof(a));
        ans = 0;
        
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }
            
        for(int i = 0;i < n;i++){
            sum = 0;
                
            for(int j = i;j < n;j++){
                sum += a[j];
                ans = max(sum, ans);
            }
        }
        
        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int n;
	for(;;){
		int row[5000];
		cin >>n;
		if(n == 0){break;}
		for(int i=0; i<n; i++){
			cin >>row[i];
		}
		int max = -9999999;
		for(int i=0; i<n; i++){
			int sum = row[i];
			if(max<sum){max = sum;}
			for(int l=i+1; l<n; l++){
				sum+=row[l];
				if(max<sum){max = sum;}				
			}
		}
		cout <<max<<endl;
	}
}

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

int main() {
  int x,n,nextsum;
  int sum,a[5000];
  while(cin >> n) {
    if(n == 0) break;
    for(int i=0;i<n;i++) {
      cin >> x;
      a[i] = x;
    }
    /*  for(int j=0;j<n;j++) {
        for(int i=j+1;j<n;i++) {
        for(int m=0;m<i-j;m++) {
        sum[k] += a[j+m];
        }
        k++;
        }
        }
        cout << *max_element(sum,sum+k) << endl;
    */
    //全パターン生成はむり
    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 <iostream>
using namespace std;

int a[5000] = {0};
int S[5000] = {0};

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

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

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

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

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