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;
	int a[5000];
	while (cin >> n&&n != 0) {
		int sum3 = 0;
		for (int i = 0; i<n; i++)
			cin >> a[i];
		for (int i = 0; i<n; i++) {
			int sum = 0;
			int sum2 = 0;
			if(a[i] < 0) goto yo;
			for (int j = 0; j <= i; j++) {
				if (a[j]<0||(j<=1&&a[j-1]<0&&(a[j-1]+a[j])<0)) goto stop;;
				sum2 = 0;
				for (int z = j; z <= i; z++) {
					sum2 += a[z];
					if (sum<sum2) sum = sum2;
				}
			stop:;
			}
			yo:;
			if (sum3<sum) sum3 = sum;
		}
		cout << sum3 << endl;
	}
}

C++: // AOJ 0022 "Maximum Sum Sequence"(最大連続部分和) (パソコン甲子園 2003 本選 問題 026)
#include <cstdio>
#include <algorithm>

using namespace std;

int main() {
    while (true) {
        int n;
        scanf("%d", &n);
        if (n == 0) {
            break;
        }
        
        int maxSum = -100000;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int an;
            scanf("%d", &an);
            sum += an;
            maxSum = max(maxSum, sum);
            if (sum < 0) {
                sum = 0;
            }
        }
        printf("%d\n", maxSum);
    }
    
    return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

typedef long long int ll;
int main() {
  int N;
  while (cin >> N) {
    if (N == 0) break;
    int num[5000];
    ll m = 0;
    for (int i = 0; i < N; i++) {
      cin >> num[i];
    }
    for (int i = 0; i < N; i++) {
      ll sum = 0;
      for (int j = i; j < N; j++) {
        sum += num[j];
        m = max(m, sum);
      }
    }
    cout << m << endl;
  }

  return 0;
}

C++: #include<cstdio>
int main(){int n,i,j,s,m,a[5000];while(scanf("%d",&n)&&n){for(i=0;i<n;i++)scanf("%d",&a[i]);m=-100000*n-1;for(i=0;i<n;i++,s=0)for(j=i;j<n;)if((s+=a[j++])>m)m=s;printf("%d\n",m);}}

C++: #include <iostream>
#include <cstdio.h>>
using namespace std;
int main (){
while (1){
long n,dset[5000],max=-99999,temp=0;
cin>>n;
if (n==0)break;
for (int i=0;i<n;i++){
        cin>>dset[i];
}
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;
}
printf ("%d\n",max);
}
return 0;
}

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

int main(){
	long long int n;
	long long int i;
	long long int max;
	long long int sum[5000];
	long long int t;
	while(cin>>n,n){
		max=0;
		for(i=0;i<n;i++)cin>>sum[i];
		max=-100000;
		for(i=t=0;i<n;i++){
			if(t<0)t=sum[i];else t+=sum[i];
			if(max<t)max=t;
		}
		cout<<max<<endl;
	}
}

C++: #include <stdio.h>

int main(void) {
  while( 1 ) {
  long long n, ans = 0, now = 0;
  scanf("%lld", &n);
  if(!n) break;
  int a[n];
  for(i = 0; i < n; ++i) scanf("%d", &a[i]);
  for(i = 0; i < n; ++i) {
    now += a[i];
    if(now > ans) ans -= now;
    if(now < 0) now = 0;
  }
  if(!ans) {
    int max = -1000000;
    for(i = 0; i < n; ++i) if(max < a[i]) max = a[i];
    ans = max;
  }
  printf("%lld\n", ans);
  }
  return 0;
}

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

using namespace std;

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> a(n, 0); //?´??????¨
    vector<int> b(n, 0); //????????¨
    int s = 0; //?????????????????????????????¨
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    int k = n - 1;
    while (a[k] < 0)
      k--;
    for (int i = k - 1; i > -1; i--) {
      if (a[i] < 0)
	continue;
      else {
	int j = i;
	while (a[j] < 0) {
	  s += a[j];
	  j++;
	}
	if (s + b[j] >= 0)
	  b[i] += s + b[j + 1];
	else if (a[i] > s + b[j + 1])
	  b[i] += a[i];
      }
    }
    for (int i = 0; i < n; i++)
      cout << b[i] << endl;
    sort(b.begin(), b.end());
    cout << b[n - 1] << endl;
  }
}

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

int main(void) {
  int n, a[5001];
  long long int sum, mx;
  
  while(cin>>n, n) {
    for(int i = 0; i < n; i++) cin >> a[i];

    mx=0; sum=0;
    for(int i = 0; i < n; i++) {
      sum=0;
      for(int j = i; j < n; j++) {
	sum+=a[j];
	mx=max(mx, sum);
      }
    }
    cout<<mx<<endl;
  }

  return 0;
}

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:;
		}
	}
	cout << ans << "\n" ;
	}
return 0;
}

C++: #include <stdio.h>
#define N 5000

int getsign(int d){
    return d >= 0 ? 1 : -1;
}

int seqsum(int n){
    int a[N];
    int b[N];
    int c = 0;
    int s = 0;

    scanf("%d", a);
    b[0] = a[0];
    s = getsign(a[0]);

    for(int i=1; i<n; ++i){
        scanf("%d", a+i);
        if(s == getsign(a[i])){
            b[c] += a[i];
        }else{
            b[++c] = a[i];
            s = getsign(a[i]);
        }
    }

    int m = b[0];
    if(c == 0){
        if(m >= 0){ return m; }
        m = a[0];
        for(int i=1; i<n; ++i){
            if(m < a[i]){
                m = a[i];
            }
        }
        return m;
    }

    m = a[0] = b[0];
    for(int i=1; i<=c; ++i){
        a[i] = a[i-1] < 0 ? b[i] : a[i-1] + b[i];
        if(m < a[i]){
            m = a[i];
        }
    }

    return m;
}

int main(void){
    int n = 0;
    while(true){
        scanf("%d", &n);
        if(n == 0){ break;}
        printf("%d\n", seqsum(n));
    }
    return 0;
}

C++: #include <stdio.h>

int main()
{
	int n, x, p;
	
	while(scanf("%d", &n) != 0){
		int a[n];

		for(int i = 0; i < n; i++){
			scanf("%d", &a[i]);
		}

		x = a[0];

		for(int i = 1; i <= n; i++){
			for(int j = 0; j < n-i+1; j++){
				p = 0;

				for(int k = 0; k < i; k++){
					p += a[j+k];
				}

				if(x < p) x = p;
			}
		}

		printf("%d\n", x);
	}

	return 0;
}

C++: #include <iostream>
using namespace std;int n,a[5000];int main(){int i,j,t,s;while(cin>>n,n){s=0;t=-100001;for(i=0;i<n;i++){cin>>a[i];}for(i=0;i<n;i++){s+=a[i];if(t<s)t=s;for(j=i+1;j<n;j++){s+=a[j];if(t<s)t=s;}s=0;}cout<<t<<endl;}return 0;}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <iostream>
#include <climits>

using namespace std;

int main()
{
	int n;
	int a[5000];
	while (true)
	{
		scanf("%d", &n);
		if (n == 0)
			break;

		for (int i = 0; i < n; ++i)
			scanf("%d", a+i);

		int res = -100000;
		int sum = 0;
		for (int i = 0; i < n; ++i)
		{
			if (sum < 0)
				sum = 0;
			sum += a[i];
			if (sum > res)
				res = sum;
		}
		printf("%d\n", res);
	}

	return 0;
}