Go: package main

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

var infty = 100000 * 2
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() (int, error) {
	scanner.Scan()
	return strconv.Atoi(scanner.Text())
}

func findMaximumSubarray(arr []int) int {
	currentMaxSum, maxSumContainsTail := -infty, -infty

	for j := range arr {
		if arr[j] > maxSumContainsTail+arr[j] {
			maxSumContainsTail = arr[j]
		} else {
			maxSumContainsTail += arr[j]
		}
		if currentMaxSum < maxSumContainsTail {
			currentMaxSum = maxSumContainsTail
		}

		// fmt.Println(arr[j], currentMaxSum, maxSumContainsTail)
	}
	return currentMaxSum
}

func main() {
	scanner.Split(bufio.ScanWords)
	for {
		size, _ := nextInt()
		if size == 0 {
			break
		}

		arr := make([]int, size)
		for i := range arr {
			n, _ := nextInt()
			arr[i] = n
		}

		maximumSum := findMaximumSubarray(arr)
		fmt.Println(maximumSum)
	}
}

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

using namespace std;

vector<int> vec;

int main(){
    
	while(true){
		int n,tmp;
		scanf("%d",&n);
		if(n==0)return 0;
		for(int i=0;i<n;i++){
			scanf("%d",&tmp);
			vec.push_back(tmp);
		}
		int sum = 0;
		int maxx = INT_MIN; 
		for(int i=0;i<vec.size();i++){
			sum = max(0,sum+vec[i]);
			maxx = max(maxx,sum);
		}
		printf("%d\n",maxx);
		vec.clear();
	}
}

C++: #include <vector>
#include <math.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;



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

		vector<int> s = vector<int>(1, 0);
		bool f = true;
		bool plus = false;
		for (int i = 0; i < n; i++)
		{
			if (a[i] >= 0)
			{
				if (!f)
					f = true;
				if (!plus)
					plus = true;
				s[0] += a[i];
			}
			else if(s[0] > abs(a[i]))
			{
				if (f)
				{
					s.push_back(s[0]);
					f = false;
				}
				s[0] += a[i];
			}
			else
			{
				s[0] = 0;
			}

		}
		if (!plus)
		{
			int max = a[0];
			for (int i = 1; i < n; i++)
			{
				if (max < a[i])
					max = a[i];
			}
			cout << max << endl;
			continue;
		}
		for (int i = 1; i < s.size(); i++)
		{
			if (s[i] > s[0])
				s[0] = s[i];
		}
		cout << s[0] << 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 = -1000000001;
    
    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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> pint;
#define rep(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};

int main(){
	int n;
	int a[5010];
	for(;cin>>n,n;){
		int max=0;
		int m=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			if(a[i]>0)m+=a[i],max=max<m?m:max;
			else if(a[i]+m>0)m+=a[i];
			else m=0;
		}
		cout<<max<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
    int n;
    int i;
    int inp,cur,max;
    while (cin>>n)
    {
        if(n == 0){
            break;
        }
        for(i=0;i<n;i++){
            cin>>inp;
            if(i == 0){
                max = inp;
                if(inp < 0){
                    cur = 0;
                }else{
                    cur = inp;
                }
            }else{
                cur = inp + cur;
                if(cur<0){
                    cur = 0;
                }else{
                    if(cur>max){
                        max = cur;
                    }
                }
            }
        }
        cout<<max<<endl;
    }

    
    return 0;
}

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


int main(){
	int n;
	int ary[5010];
	int sum[5020] = { 0 };

	while (cin >> n, n != 0){
		int max_ = numeric_limits<int>::min();
		for (int i = 0; i < n; i++){
			cin >> ary[i];
		}
		for (int i = 1; i < n+1; i++){
			sum[i] = sum[i - 1] + ary[i-1];
		}
		for (int i = 1; i < n + 1; i++){
			for (int j = i; j < n + 1; j++){
				max_ = max(max_, sum[j] - sum[i - 1]);
			}
		}
		cout << max_ << endl;
	}

	return 0;
}

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

int main(){
  long max;
  char str[10];
  int i, j, k;
  int num, nnum;
  long setdata[2][5001], tmp;
  int focus;

  while(cin>>num){
    if(num==0) break;
    for(i=0; i<num; i++){
      cin>>str;
      setdata[0][i]=(long)atol(str);
    }

    max=100001*(-1);
    for(i=0; i<num; i++){
      if(max<setdata[0][i]) max=setdata[0][i];
    }
    focus=1;
    for(i=num-1; i>=1; i--){
      for(j=0; j<i; j++){
        if(i==num-1){
          setdata[1][j]=setdata[0][j]+setdata[0][j+1];
          if(max<setdata[1][j]) max=setdata[1][j];
        }
        else {
          setdata[focus][j]=setdata[1-focus][j]+setdata[1-focus][j+1]-setdata[focus][j+1];
          if(max<setdata[focus][j]) max=setdata[focus][j];
        }
      }
      focus=1-focus;
    }
    printf("%ld\n", max);
  }

  return 0;
}

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

int main(void) {
    int sum, sum_max;
    int n;
    
    while(cin >> n) {
        if(n == 0) {
            break;
        }
        int array[n] = {};
        for(int i = 0; i < n; ++i) {
            cin >> array[i];
        }
        sum_max = array[0];
        for(int i = 0; i < n; ++i) {
            sum = 0;
            for(int j = i; j < n; ++j) {
                sum += array[j];
                if(sum_max < sum) {
                    sum_max = sum;
                }
            }
        }
        cout << sum_max << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;
    while(cin>>n,n){
        vi v(n+1,0);
        rep(i,n)cin>>v[i];
        ll ans=-INF, temp;
        for(int i=0;i<n;i++){
            temp=0;
            for(int j=i;j<n;j++){
                temp+=v[j];
                ans=max(ans,temp);
            }
        }
        if(n==1)cout<<v[0]<<endl;
        else cout<<ans<<endl;
    }
}