Go: package main

import (
        "fmt"
)

func main() {
        for {
                var n int
                _, err := fmt.Scanf("%d", &n)
                if err != nil || n == 0 {
                        break
                }

                var sum [5001]int

                for i := 1; i <= n; i++ {
                        fmt.Scanf("%d", &(sum[i]))
                        if i > 1 {
                                sum[i] += sum[i-1]
                        }
                }

                max_sum := sum[1]
                for i := 1; i <= n; i++ {
                        for j := 0; j < i; j++ {
                                if max_sum < sum[i] - sum[j] {
                                        max_sum = sum[i] - sum[j]
                                }
                        }
                }
                fmt.Printf("%d\n", max_sum)
        }
}

C++: #include <stdio.h>
#include <string.h>

int n ;
int p ;
int q ;
int numlist[5010] ;
bool changed ;
bool vis[5010] ;

void proc ( ) ;

int main ( )
{
	while ( scanf ( "%d" , &n ) == 1 && n ) {
		for ( int i = 0 ; i < n ; i++ ) {
			scanf ( "%d" , &numlist[i] ) ;
		}
		p = 0 ;
		q = n - 1 ;
		changed = true ;
		memset ( vis , false , sizeof ( false ) ) ;
		while ( numlist[p] <= 0 ) {
			p++ ;
		}
		while ( numlist[q] <= 0 ) {
			q-- ;
		}
		while ( changed == true && p < q ) {
			changed = false ;
			proc ( ) ;
		}
		int total = 0 ;
		for ( int i = p ; i <= q ; i++ ) {
			total += numlist[i] ;
		}
		printf ( "%d\n" , total ) ;
	}
	return 0 ;
}

void proc ( )
{
	for ( int i = p ; i <= q ; i++ ) {
		if ( numlist[i] < 0 && vis[i] == false ) {
			vis[i] = true ;
			int tmp = 0 ;
			for ( int j = p ; j < i ; j++ ) {
				tmp += numlist[j] ;
			}
			if ( tmp < numlist[i] * ( -1 ) ) {
				p = i + 1 ;
				changed = true ;
			}
			break ;
		}
	}
	for ( int i = q ; i >= p ; i-- ) {
		if ( numlist[i] < 0 && vis[i] == false ) {
			vis[i] = true ;
			int tmp = 0 ;
			for ( int j = q ; j > i ; j-- ) {
				tmp += numlist[j] ;
			}
			if ( tmp < numlist[i] * ( -1 ) ) {
				q = i - 1 ;
				changed = true ;
			}
			break ;
		}
	}
}

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

double dot(P a,P b){return real(conj(a)*b);};
double cross(P a,P b){return imag(conj(a)*b);};

int main(){
  int n;
  while(~scanf("%d",&n)){
    if(n==0)break;
    ll cur;
    cin>>cur;
    ll mx = cur;
    while(--n){
      ll x;
      cin>>x;
      cur = max(x,cur+x);
      mx = max(mx,cur);
    }
    cout<<mx<<endl;
  }
  return 0;
}

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

using namespace std;

int N;
long long K[5001],T;

int main()
{
        scanf("%d",&N);
        if (N == 0) return 0;

        T = 0;
        K[0] = 0;

        for (int i=1;i<=N;i++){
                scanf("%lld",K+i);
        }

        for (int i=1;i<=N;i++) {
                K[i] += K[i-1];
        }

        for (int i=0;i<N;i++) {
                for (int j=i+1;j<=N;j++) {
                        T = max(T,K[j]-K[i]);
                }
        }

        printf("%lld\n",T);

        main();
}

C++: #include<iostream>
#define INFTY (1<<21)
using namespace std;

void maxsequence(int n) {
    int a[n], s[n], result = -INFTY;
    for ( int i = 0; i < n; i++ ) cin >> a[i];
    s[0] = a[0];
    for ( int i = 1; i < n; i++ ) s[i] = max(a[i], a[i] + s[i - 1]);
    for ( int i = 0; i < n; i++ ) result = max(result, s[i]);
    cout << result << endl;
}

int main() {
  int n;
  while ( cin >> n ) {
      if ( n == 0 ) return 0;
      maxsequence(n);
  }
}

C++: #include <iostr;eam>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  vector<int> maxsums, a;
  int n, tmp;

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      a.push_back(tmp);
    }

    for (int i = 0; i < a.size(); i++) {
      tmp = a[i];
      for (int j = i + 1; j < a.size(); j++) {
        tmp += a[j];
        if (tmp > a[i])
          a[i] = tmp;
      }
    }

    int max = a[0];
    for (int i = 0; i + 1 < a.size(); i++)
      if (max < a[i])
        max = a[i];
    maxsums.push_back(max);
    a.clear();
  }

  for (int i = 0; i < maxsums.size(); i++)
    cout << maxsums[i] << endl;
  return 0;
}

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

using namespace std;

template <typename Type>
long long Maximum_Sum_Sequence(Type N[], int size)
{
	long long max = LLONG_MIN;

	for (int i = 0; i < size; i++)
	{
		long long sum = 0;

		for (int j = i; j < size; j++)
		{
			sum += N[j];

			if (sum > max) { max = sum; }
		}
	}

	return max;
}

int main()
{
	const int DATASETS = 5000;

	int n[DATASETS], a[DATASETS][5000], c = 0;

	while (true)
	{
		cin >> n[c];

		if (n[c] == 0) { break; }

		for (int i = 0; i < n[c]; i++) { cin >> a[c][i]; }

		c++;
	}

	for (int i = 0; i < c; i++)
	{
		cout << Maximum_Sum_Sequence(a[i], n[i]) << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#define INF 1e+18
#define MAX_SIZE 1 << 17
using namespace std;

typedef long long ll;

int n,k;
ll v[100000];
ll segMax[2][2 * MAX_SIZE - 1],segAdd[2][2 * MAX_SIZE - 1];

void add(int num,int a,int b,ll x,int k = 0,int l = 0,int r = n){
	if(b <= l || r <= a) return;
	if(a <= l && r <= b){
		segAdd[num][k] += x;
		return;
	}
	add(num,a,b,x,k * 2 + 1,l,(l + r) / 2);
	add(num,a,b,x,k * 2 + 2,(l + r) / 2,r);
	segMax[num][k] = max(segMax[num][k * 2 + 1] + segAdd[num][k * 2 + 1],segMax[num][k * 2 + 2] + segAdd[num][k * 2 + 2]);
}

ll get(int num,int a,int b,int k = 0,int l = 0,int r = n){
	if(b <= l || r <= a) return -INF;
	if(a <= l && r <= b) return segAdd[num][k] + segMax[num][k];
	return max(get(num,a,b,k * 2 + 1,l,(l + r) / 2),get(num,a,b,k * 2 + 2,(l + r) / 2,r)) + segAdd[num][k];
}

void init(){
	int t = 1;
	while(t < n + 1) t *= 2;
	for(int i = 0;i < t * 2 - 1;i++){
		segMax[0][i] = -INF;
		segMax[1][i] = -INF;
	}
	segMax[0][t - 1] = 0;
	for(int i = 0;i < n;i++) segMax[0][i + t] = segMax[0][i + t - 1] + v[i];
	ll ma = -INF;
	for(int i = n;i >= 0;i--) {
		ma = max(ma,segMax[0][i + t - 1]);
		segMax[1][i + t - 1] = ma - segMax[0][i + t - 1];
	}
	for(int i = t - 2;i >= 0;i--) {
		segMax[0][i] = max(segMax[0][i * 2 + 1],segMax[0][i * 2 + 2]);
		segMax[1][i] = max(segMax[1][i * 2 + 1],segMax[1][i * 2 + 2]);
	}
	n = t;
}

int main(){
	while(1){
	//scanf("%d %d",&n,&k);
	scanf("%d",&n);
	if(!n) break;
	for(int i = 0;i < n;i++) scanf("%lld",&v[i]);
	init();
	for(int i = 0;i < k - 1;i++){
		
	}
	printf("%lld\n",get(1,0,n));
	}
	return 0;
}

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

int main() {

  long long int n = 0;

  while( cin >> n, n) {

    long long int max_ = 0;

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

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

  return 0;

}

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

int main() {
	int n;
	long long sum, max;

	while (true) {
		sum = 0;
		max = -10000000;
		cin >> n;
		if (n == 0) break;

		int a[n];

		for (int i = 0; i < n; i++)
			cin >> a[i];

		for (int i = 0; i < n; i++) {
			sum = 0;
			for (int j = 1; j <= n; j++) {
				if (i + j - 1 < n) {
					sum += a[i + j - 1];
					if (sum > max)	max = sum;
				}
			}
		}

		cout << max << endl;
	}
	return 0;
}