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<vector>
#include<iostream>

using namespace std;

int main(){
	while(true) {
		long long int n, tmp;
		cin >> n;
		if (n<=0) {
			//cout << 0 << endl;
			return 0;
		}
		vector<long long int> intervalos;
		intervalos.reserve(n);
		cin >> tmp;
		bool esNegativo = tmp < 0;
		intervalos.push_back(tmp);
		for (int i = 1; i < n; ++i){
			cin >> tmp;
			if (esNegativo == tmp < 0){
				intervalos.back() += tmp;
			} else {
				intervalos.push_back(tmp);
				esNegativo ^= true;
			}
		}
		long long int best = 0;
		long long int current = 0;
		for (int i = 0; i < intervalos.size(); ++i){
			if (intervalos[i]+current <= 0) {
				current = 0;
			} else {
				current+=intervalos[i];
				if (current > best) {
					best=current;
				}
			}
		}
		intervalos.clear();
		cout << best << endl;
		}
	
}

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

int main(){
	int n;
	while(cin >> n, n){
		/*n++;
		vector<int> t(n), v(n);
		t[0] = 0;
		for(int i = 1;i < n;i++){
			cin >> v[i];
			t[i] = max(t[i-1]+v[i], v[i]);
		}*/
		vector<int> vec(n),dp(n);
		for(int i=0;i<n;i++)cin >> vec[i];
		dp[0]=vec[0];
		for(int i=1;i<n;i++)dp[i]=max(dp[i-1]+vec[i],vec[i]);
		cout << *max_element(dp.begin(), dp.end()) << endl;
	}
	return 0; 
}

/*
7
-5
-1
6
4
9
-6
-7
*/

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

int main(){

	int n;
	while (std::cin >> n, n){

		int max, before;
		std::cin >> before;

		max = before;

		for (int i = 1, a; i < n; i++){
			std::cin >> a;
			before = std::max(before + a, a);
			max = std::max(max, before);
		}

		std::cout << max << std::endl;
	}

	return 0;
}

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

int main() {
	int n, a;
	vector<int> an;
	vector<vector<int>> AN;
	while (cin >> n) {
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> a;
			an.push_back(a);
		}
		AN.push_back(an); an.clear();
	}

	for (int i = 0; i < AN.size(); i++) {
		int max = 0, MAX = 0 , counter = 0;
		for (int j = 0; j < AN[i].size(); j++) {
			if (AN[i][j] >= 0) {
				max += AN[i][j];
				if (MAX < max)MAX = max;
				counter = 1;
			}
			else {
				if (counter==1){
					if (AN[i][j] + max >= 0)max += AN[i][j];
					else {
						max = 0;
						counter = 0;
					}
				}
			}
		}
		cout << MAX << endl;
	}
	
	return 0;
}

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

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 sum[5001];
int main(){
	int N;
	int ans = 0;
	while(cin>>N&&N){
		int index=0;
		ans = INT_MIN;
		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-1]);
			}
		}
		cout<< ans <<endl;
	}
}

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

int main(){
    int n;
    while(cin >> n, n){
        vector<int> a(n,0);
        for(i=0; i<n; ++i){
            cin >> a[n];
        }
        // ????????£?¶??????????????????§??£?¶???????dp??¨?????????
        vector<int> dp(n,0);
        dp[0] = a[0];
        int max = 0;
        for(i=0; i<n; ++i){
            if(dp[i-1]>0){
                dp[i] = dp[i-1] + n[i];
            } else {
                dp[i] = n[i];
            }
            if(max<dp[i]){
                max = dp[i];
            }
        }
        cout << max << endl;
    }
}

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

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

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

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

  int maxsum = INT_MIN;
  int sum;
  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;
    }
  }
  cout << maxsum << endl;
  }
}

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 = -1000001;
    
    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(void)
{
    int n, a[5000];
    while (cin >> n && n!=0) {
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        int max = -1e6;
        for (int i = 0; i < n; ++i) {
            int sum = 0;
            for (int j = i; j < n; ++j) {
                sum += a[j];
                if (sum > max)
                    max = sum;
            }
        }
        cout << max << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
#define int long long
signed main(){
        int n;
        while( scanf("%lld", &n) != EOF ){
                if(n==0) break;
                int max=-100001;
                int now=0;
                for(int i=0;i<n;i++){
                        int t;
                        scanf("%lld", &t);
                        if(now+t>t)now+=t;
                        else now = t;
                        if(now>max) max=now;
                }
                printf("%lld\n",max);
        }
        return 0;
}

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

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