Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func main() {
	sum := make([]int, 5001)
	for {
		n := nextInt()
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			a := nextInt()
			sum[i+1] = sum[i]+a
		}
		ret := sum[1]
		for i := 0; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				ret = max(ret, sum[j]-sum[i])
			}
		}
		fmt.Println(ret)
	}
}

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 ) {
			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<iostream>
#include<algorithm>
using namespace std;
//??????????????¢????????£?????????????????????
int main(){
	int nn[5001] = { 0 }, temp, n, max = -110000;
	while (cin >> n && (n != 0)){
		for (int i = 0; i < n; i++){
			cin >> nn[i];
		}
		temp = nn[0];
		for (int i = 1; i < n; i++){
			temp += nn[i];
			if (temp < nn[i]) temp = nn[i];
			if (max < temp) max = temp;
		}if (max < nn[0]) cout << nn[0] << endl;
		else cout << max << endl;
		max = -110000;
	}
	return 0;
}

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

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

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

int main(){
  vector<int> v,s(1001);
  int a,b,o=0;
  while(cin>>b){
    fill(s.begin(),s.end(),0);
    o =0;
    if(b==0) break;
    else{
    for(int i=0;i<b;i++){
      cin>>a;
      v.push_back(a);
    }
      for(int i = 0; i < b; i++){
		int sum = 0;
		for(int j = i; j < b; j++){
  sum += v[j];
  s.push_back(sum);
		}
	  }


  int d = *max_element(s.begin(),s.end());
  cout<<d<<endl;
    }
  }
}

C++: #include<stdio.h>

int main(){
	int n,a[5000],b[5000],i,j,max;
	
	while(0<=scanf("%d",&n)){
		if(n==0)break;
		max=0;
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
			if(max<a[i])max=a[i];
		}
		b[0]=a[0];
		for(i=1;i<n;i++){
			b[i]=b[i-1]+a[i];
			if(b[i]>max)max=b[i];
		}
		
		for(i=1;i<n;i++){
			for(j=i;j<n;j++){
				if(max<b[j]-b[i-1])max=b[j]-b[i-1];
			}
		}
		
		printf("%d\n",max);
	}
	return 0;
}

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

using namespace std;

int main()
{
    int n;

    while (scanf("%d", &n), n) {
        int s[5001] = {};

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

        int ma = INT_MIN;

        for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) {
            ma = max(ma, s[i] - s[j - 1]);
        }

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

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<bits/stdc++.h>
using namespace std;
int a[5000],n;
signed main(){
    while(cin>>n,n){
        for(int i=0;i<n;i++)cin>>a[i];
        int ma=LLONG_MIN;
        for(int i=0;i<n;i++){
            int cur=0;
            for(int j=i;j<n;j++){
                cur+=a[j];
                ma=max(ma,cur);
            }
        }
        cout<<ma<<endl;
    }

    return 0;
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <stack>
#include <cstring>

using namespace std;

int main(){
	int n;
	int max,tmpmax1,tmpmax2;
	while(cin >> n && n != 0){
		max = -100001;
		int *factor = new int[n];
		
		for(int i = 0; i < n; i++){
			cin >> factor[i];
		}

		for(int i = 0; i < n - 1; i++){
			tmpmax1 = 0;
			tmpmax2 = -100001;
			for(int j = i; j < n; j++){
				tmpmax1 += factor[j];
				if(tmpmax2 < tmpmax1){
					tmpmax2 = tmpmax1;
				}
			}
			if(max < tmpmax2){
				max = tmpmax2;
			}
		}
		delete [] factor;
		cout << max << endl;
	}
	
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<cctype>
#include<iomanip>
#include<functional>
#include<numeric>
#define EPS 1e-10
#define INF (-(1<<21))
using namespace std;
typedef long long llong;
int main() {
	int n;
	while (1) {
		cin >> n;
		if (!n)break;
		vector<vector<llong> >v(n, vector<llong>(n));
		vector<int>a(n);
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j)v[i][j] = a[i];
				else v[i][j] = INF;
			}
		}
		int max = INF;
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				if (i == j) {
					//cout << v[i][j] << " ";
					continue;
				}
				v[i][j] = v[i][j - 1] + a[j];
				if (max < v[i][j])max = v[i][j];
				//cout << v[i][j] << " ";
			}
			//cout << endl;
		}
		cout << max << endl;
	}
	return 0;
}

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

// warning: include *e
int getBackMaxSum(int* s, int *e) {
	int sum = *e, maxSum = sum;
	for (e--; s <= e; e--) {
		sum += *e;
		maxSum = max(sum, maxSum);
	}
	return maxSum;
}

int main () {
	while (cin >> n, n) {
		int a[n], maxSum = 0x80000000;
		cout << maxSum << endl;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			maxSum = max(maxSum, getBackMaxSum(a, a + i));
		}
		cout << maxSum << endl;
	}
	return 0;
}

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


int main(void) {

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

    vector<int> a(n+1, 0);
    int sum = 0;
    for(int i=0; i<n; i++){
      int tmp;
      cin >> tmp;
      sum += tmp;
      a[i+1] += sum;
    }

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

  }

  return 0;
}