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<iostream>
#include<vector>
int main()
{
	while (!std::cin.eof())
	{
		int n;
		std::cin >> n;
		if (n == 0)
		{
			break;
		}
		std::vector<int>a;
		int max = -100001;
		bool is_all_minus = true;
		for (int i = 0; i < n; i++)
		{
			int foo;
			std::cin >> foo;
			if (foo < 0 && max < foo)
			{
				max = foo;
			}
			is_all_minus &= foo < 0;
			if (i == 0 || a.back() * foo < 0 || a.back() < 0 && foo == 0 || a.back() == 0 && foo < 0)
			{
				a.push_back(foo);
			}
			else
			{
				a.back() += foo;
			}
		}
		if (!is_all_minus)
		{
			if (a.size() == 1)
			{
				max = a[0];
			}
			else
			{
				int begin = a[0] >= 0 ? 0 : 1;
				int temp_max = a[begin];
				try
				{
					for (int i = begin;;)
					{
						if (a.at(i + 1) + a.at(i + 2) > 0)
						{
							temp_max += a[i + 1] + a[i + 2];
							i += 2;
						}
						else
						{
							if (max < temp_max)
							{
								max = temp_max;
							}
							temp_max = a[i + 2];
							i += 2;
						}
					}
				}
				catch (std::out_of_range) {}
				if (max < temp_max)
				{
					max = temp_max;
				}
			}
		}
		std::cout << max << std::endl;
	}
	return 0;
}

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

int main()
{
	int n=1, count = 0;
	int a[10000];
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		int sum = 0, max = -100000;
		for (int ai = 0; ai < n; ai++) {
			sum = 0;
			for (int bi = ai; bi < n; bi++) {
				sum = sum + a[bi];
				if (sum > max) {
					max = sum;
				}
			}
		}
		if (n == 0)
			break;
		cout << max << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, const char * argv[])
{
    while (1) {
        int n; scanf("%d", &n);
        if (n == 0) break;
        long int a[n], b[n], tmp = 0;
        for (int i = 0; i < n; i++) {
            scanf("%ld", &a[i]);
            b[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            for (int k = 0; k <= i; k++) {
                b[i] += a[k];
            }
        }
        for (int i = n-1; i > 0; i--) {
            for (int k = i-1; k >= 0; k--) {
                if (i == n-1) {
                    tmp = (b[i] - b[k] + a[k]);
                }
                if (tmp < (b[i] - b[k] + a[k])) {
                    tmp = (b[i] - b[k] + a[k]);
                }
            }
        }
        cout << tmp << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int main (){
while (1){
long n,dset[5000],temp=0,max;
cin>>n;
if (n==0)break;
for (int i=0;i<n;i++){
        cin>>dset[i];
}
max=-1000000;
for (int i=0;i<n;i++){
        for (int j=i;j<n;j++){
                temp=temp+dset[j];
                if (max<temp){
                        max=temp;
                }
        }
temp=0;
}
cout<<max<<endl;
}
return 0;
}

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

using namespace std;

int main(int argc, char *argv[]) {
  vector<int> maxsums, a;
  int n, tmp;

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      a.push_back(tmp);
    }

    for (int i = 0; i < a.size(); i++) {
      tmp = a[i];
      for (int j = i + 1; j < a.size(); j++) {
        tmp += a[j];
        if (tmp > a[i])
          a[i] = tmp;
      }
    }

    int max = a[0];
    for (int i = 0; i + 1 < a.size(); i++)
      if (max < a[i])
        max = a[i];
    maxsums.push_back(max);
    a.clear();
  }

  for (int i = 0; i < maxsums.size(); i++)
    cout << maxsums[i] << endl;
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template<class T> using vv=vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int n;

int calc(int s, int a[], int b[]){
    if(b[s] != 0) {
        return b[s];
    }

    if(s == n-1) {
        return b[n-1] = max(0, a[n-1]);
    }
    return b[s] = max(a[s]+calc(s+1,a,b), 0);
}

int main() {
    while(1) {
        int a[5000] = {};
        int b[5000] = {};
        bool f = false;
        int max = -100000;
        cin >> n;
        if(n == 0) {
            break;
        }
        rep(i,n) {
            cin >> a[i];
            if(a[i] >= 0 && f == false) {
                f = true;
            }
        }

        if(f == false) {
            rep(i,n){
                if(a[i] > max) {
                    max = a[i];
                }
            }
            cout << max << endl;
            continue;  
        }

        calc(0,a,b);
        FOR(i,0,n) {
            if(b[i] > max) {
                max = b[i];
            }
        }
        cout << max << endl;
    }
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;
typedef long long ll;

#define PI 3.141592653

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

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

int main(){
	
	int n;
	int in[6000],ans;
	
	while(cin>>n,n){
		for(int i=0;i<n;i++){
			cin>>in[i];
		}
		ans=0;
		for(int i=0;i<n;i++){
			int sum=0;
			for(int j=i;j<n;j++){
				sum=sum+in[j];
				ans=max(sum,ans);
			}
		}
		
		cout<<ans<<endl;
	}
	return 0;
}

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;b<=T;b++){
				res=max(sum(b,bit) - sum(a,bit),res);
			}
			//cout << sum(a,bit) << endl;
		}
		
		printf("%d\n",res);
	}
}