Go: package main

import "fmt"

func main() {
	var result []int
	for {
		var n, a, maxSum int
		fmt.Scan(&n)

		if n == 0 {
			break
		}

		maxSum = 0
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			if i == 0 {
				sum = a
				maxSum = sum
			} else if sum+a < a {
				sum = a
				if maxSum < sum {
					maxSum = sum
				}
			} else {
				sum = sum + a
				if maxSum < sum {
					maxSum = sum
				}
			}
		}

		result = append(result, maxSum)
	}

	for _, v := range result {
		fmt.Println(v)
	}
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>

typedef long long ll;
using namespace std;


int sum(int i,int* bit){
	int s = 0;
	while(i > 0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int i, int x,int* bit,int n){
	while(i <= n){
		bit[i] += x;
		i += i & -i;
	}	
}

int main()
{
	int T;
	while(~scanf("%d",&T)){
		if(T==0) break;
		int* bit = new int[T+1]();	
		int res=numeric_limits<int>::min();
		for(int t=1;t<=T;t++){
			int tmp;
			scanf("%d",&tmp);
			add(t,tmp,bit,T);
		}

		for(int a=0;a<=T;a++){
			for(int b=a+1;b<=T;b++){
				res=max(sum(b,bit) - sum(a,bit),res);
			}
			//cout << sum(a,bit) << endl;
		}
		
		printf("%d\n",res);
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  int n;
  while(cin >>n,n){
    vector<long int> a,b;
    for(int i=0,j; i<n; i++){
      cin >>j;
      a.push_back(j);
      if(i != 0) a[i]+=a[i-1];
    }
    b.push_back(0);
    for(int i=0; i<n; i++) b.push_back(min(b[i],a[i]));
    long int ans = a[0];
    for(int i=1; i<n; i++) ans = max(ans,a[i]-b[i]);
    cout <<ans<<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) V.push_back(0);

	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>
#include <queue>
#include <algorithm>

using namespace std;

int main() {
    while(true) {
        int N;
        cin >> N;
        if(!N) break;

        queue<int> q;
        int ans = -100000;
        int sump = 0, sumn = 0;
        int left = 0;
        while(N--) {
            int n;
            cin >> n;
            q.push(n);
            if(n >= 0) sump += n;
            else sumn += -n;
            while(!q.empty() && sump <= sumn) {
                int np = q.front();
                if(np >= 0) sump -= np;
                else sumn -= -np;
                q.pop();
            }
            ans = max(ans, sump-sumn);
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main(void) {
    int num;
    while(cin >> num, num != 0) {
        int ary[num + 1];
        ary[0] = 0;
        for(int r = 1; r <= num; r++) {
            int input; cin >> input;
            ary[r] = ary[r - 1] + input;
        }
        int max = ary[1];
        for(int r = 1; r <= num; r++) {
            for(int c = 0; c <= r; c++) {
                int val = ary[r] - ary[c];
                if(val > max) max = val;
            }
        }
        
        cout << max << endl;
    }
    return(0);
}

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

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

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

using namespace std;

int debug = 0;

int main(void){
	int n, m, i, j, k;
	int tmp, max;
	bool first = 0;
	vector<int> intv;

	while(cin >> n){
		if(n == 0){
			break;
		}else{
			//ÇÝÝ
			for(i=0;i<n;i++){
				cin >> m;
				intv.push_back(m);
				//if(debug) cout << "int[" << i << "] =" << intv[i] << endl; 
			}

			//vfð¸ç·
			tmp=0;
			for(i=1;i<intv.size();i++){
				if(intv[tmp] > 0){
					if(intv[i] > 0){ //¯Èç
						intv[tmp] += intv[i];
						intv[i] = 0;
					}else{ //ÙÈç
						tmp = i;
					}
				}else if(intv[tmp] < 0){
					if(intv[i] < 0){
						intv[tmp] += intv[i];
						intv[i] = 0;
					}else{
						tmp = i;
					}
				}
			}
			/*
			if(debug){
				for(i=0;i<intv.size();i++){
					cout << intv[i] <<endl;
				}
				cout << "----------------------" << endl;
			}
			*/
			intv.erase( remove(intv.begin(), intv.end(), 0), intv.end());
			/*
			if(debug){
				for(i=0;i<intv.size();i++){
					cout << intv[i] <<endl;
				}
			}
			*/

			int vsize = intv.size();
			for(i=1;i<=vsize;i+=2){//i:Â
				//if(debug) cout << "number =" << i << endl;
				for(j=0;j<=vsize-1-i+1;j++){ //j:JnÊu
					tmp = 0;
					for(k=0;k<i;k++){
						tmp += intv[j+k];
					}
					//if(debug) cout << "tmp ==" << tmp << endl;
					if(first == false){
						max = tmp;
						first = true;
					}else if(max < tmp){
						max = tmp;
					}
				}
			}
			cout << max << endl;
			intv.clear();
			first = false;
		}
	}
	return 0;
}

C++: #include<stdio.h>

int N[5001]={};
int main(){

int n,x;

while(1){
scanf("%d",&n);if(n==0)break;


for(int i=0;i<n;i++)
scanf("%d",&N[i]);
int t=0,s=N[0];
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
s+=N[j];if(t<s)t=s;
}
s=0;
}
printf("%d\n",t);
}
return 0;
}