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 <bits/stdc++.h>
using namespace std;

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

int dx[] = { -1, 0, 1, 0 };
int dy[] = {  0, -1, 0, 1 };

int main() {

  int n;
  loop {
    cin >> n;
    if (!cin) break;
    if (!n) break;

    int ac = 0;
    vi xs;
    rep (_, n) {
      int x; cin >> x;
      ac = max(x, ac+x);
      xs.push_back(ac);
    }

    int mx = 0;
    for (int x:xs) mx = max(x,mx);
    cout << mx << endl;

  }

  return 0;
}

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

int main(void){
	int N;
	while (cin >> N, N) {
		vector<int> a(N);
		for (int i = 0; i < N; i++)
			cin >> a[i];
		vector<int> sums;
		sums.push_back(0);
		partial_sum(a.begin(), a.end(), back_inserter(sums));

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

		cout << m << endl;
	}
}

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

int main(){
  int n;
  cin >> n;
  while(n!=0){
    vector<int> a(n);
    for(int i=0;i<n;i++) cin >> a.at(i);

    vector<int> s(n+1);
    s.at(0) = 0;
    for(int i=1;i<n+1;i++) s.at(i) = s.at(i-1)+a.at(i-1);

    int ans= -1000000000;
    for(int i=1;i<n+1;i++){
      for(int j=0;j<i;j++){
        ans = max(s.at(i)-s.at(j),ans);
      }
    }

    cout << ans << endl;

    cin >> n;
  }
}

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

#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int main() {
  
  int N;
  while(cin >> N && N) {
    vector<int> v(N);
    rep(i, N) cin >> v[i];
    
    ll mx = -1<<29;
    rep(i, N) {
      ll sum = 0;
      REP(j, i, N) {
	sum += v[j];
	mx = max(mx, sum);
      }
    }
    cout << mx << endl;
  }
  
  return 0;
}

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

using namespace std;

int main(){

  int n;


  while(cin >> n){
    if(n == 0) break;
    vector<int> V;
    int status = 1;
    int in;
    int sum = 0;
    for(int i = 0; i < n; i++){
      cin >> in;
      if(i == 0){
	if(in < 0) status = -1;
	else status = 1;

      } else {	
	if(in < 0 && status == 1){
	  status = -1;
	  V.push_back(sum);
	  sum = 0;
	}else if(in > 0 && status == -1){
	  status = 1;
	  V.push_back(sum);
	  sum = 0;
	}
      }
      sum += in;
    }

    V.push_back(sum);

    int max = -700000000;
    
    for(int i = 0; i < V.size(); i++){
      sum = 0;
      for(int j = i; j < V.size(); j++){
	sum += V[j];
	if(max < sum) max = sum;
      }
    }
    
    cout << max << endl;
  }
  return 0;
}

C++: #include<iostream>[
using namespace std;
int main(){
    int n;
    while(cin>>n,n){
        int a[5000];
        for(int i=0;i<n;i++)cin>>a[i];
        int mxm=-1000001;
        for(int i=0;i<n;i++){
            int b=0;
            for(int j=i;j<n;j++){
                b+=a[j];
                mxm=max(mxm,b);
            }
        }
        cout<<mxm<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int main()
{
	int n, k, p = 0;
	bool isPlus = true;
	while (cin >> n){
		if (0 == n) break;
		vector<int> A(n, 0);
		int m = INT_MIN;
		for (int i = 0; i < n; ++i){
			cin >> k;
			if (0 == i){
				A[p] = k;
				isPlus = (k > 0);
			}
			else {
				if (isPlus == (k > 0)){
					A[p] += k;
				}
				else{
					if (isPlus == (k > 0)){
						A[p] += k;
					}else{
						++p;
						A[p] = k;
						isPlus = !isPlus;
					}
					while (1 < p && isPlus){
						if (m < A[p - 2]) m = A[p - 2];
						if (m < A[p]) m = A[p];
						int a = A[p - 2] + A[p - 1] + A[p];
						if (a > A[p-2] && a > A[p]){
							A[p - 2] += A[p-1] + A[p];
							A[p - 1] = A[p] = 0;
							p -= 2;
							isPlus = true;
						}
						else break;
					}
					if (m < A[p]) m = A[p];
				}
			}
		}
		if (m < A[p]) m = A[p];
		cout << m << "\n";
	}
	return 0;
}

C++: include<iostream>
#include<algorithm>
using namespace std;
int main(){
	while(1){
		int n;
		int a[5001];
		cin>>n;
		if(n==0)break;
		int b;
		a[0]=0;
		cin>>a[1];
		for(int i=2;i<=n;i++){
			cin>>b;
			a[i]=a[i+1]+b;
		}
		int ans=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<i;j++){
				ans=max(ans,a[i]-a[j]);
			}
		}
		cout<<ans<<endl;
	}
}

C++: #include <stdio.h>

int main(void) {

  int n;

  int a[6000];

  while(1) {

    int max_a=0;
    int sum;

    int i,j;

    scanf("%d",&n);
    getchar();

    if(n==0) break;

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

    //for(i=0;i<n;i++) printf("%d ",a[i]);

    for(i=0;i<n;i++) {

      sum=0;

      for(j=i;j<n;j++) {

	sum+=a[j];

	if(max_a<sum) max_a=sum;
	//printf("%d ",max_a);
      }

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

  }



  return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;

/*
*/

ll n,a[5010],dp[5010],ans;

int main(void){
	while(1){
		cin>>n;
		if(n==0)break;
		reg(i,1,n)cin>>a[i];
		dp[0]=0;
		reg(i,1,n)dp[i]=max(dp[i-1]+a[i],a[i]);
		ans=-1e18;
		reg(i,1,n)ans=max(ans,dp[i]);
		cout<<ans<<endl;
	}
	return 0;
}

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

using namespace std;

int main(){
  int n, x, max_;
  int dp[5001];
  while(1){
    cin >> n;
    if(!n) break;
    max_=0; dp[0]=0;
    for(int i=1; i<=n; ++i){
      cin >> x;
      dp[i] = max(x, dp[i-1]+x);
      max_ = max(max_, dp[i]);
    }
    cout << max_ << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int A[50000]={0};
int S[50000]={0};

int main(){
  int n;

  while(cin >> n&&n>0){
    for(int i=0;i<n;++i) cin >> A[i];

    long maxsum = 0;
    for(int i=0;i<n;++i){
      for(int j=i;j<n;++j){
        long sum = 0;
        for(int k=i;k<=j;++k){
          sum += A[k];
        }
        if(sum>maxsum) maxsum = sum;
      }
    }
    cout << maxsum << endl;
  }

  return 0;
}