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)
		{
			max = -100001;
			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 == -100001)
					{
						max = temp_max;
					}
				}
			}
		}
		std::cout << max << std::endl;
	}
	return 0;
}

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

typedef long long ll;
const ll INF = 1000000000;

int main()
{
	ll t, n;
	while( 1 )
	{
		scanf("%lld", &t);
		if(t == 0) break;

		ll c_max = -INF;
		ll c_min = 0;
		ll c_sum = 0;
		for(int i=0; i<t; i++)
		{
			scanf("%lld", &n);
			c_sum += n;
			if(c_max < c_sum - c_min) c_max = c_sum - c_min;
			if(c_min > c_sum) c_min = c_sum;
			
		}
		printf("%lld\n", c_max);
	}
}

C++: #include <stdio.h>

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

    int a[5000];
    int max;

    int i,j,k;
    for(i=0; i<n; ++i){
      scanf("%d", &a[i]);
      for(j=0; j<=i; ++j){
        int sum = 0;
        for(k=j; k<=i; ++k){
          sum += a[k];
        }
        if((i==0 && j==0) || max<sum){
          max = sum;
        }
      }
    }

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

  return 0;
}

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

using namespace std;

typedef long long LINT;

const int MAX_N = 5000;

LINT as[MAX_N+1];

int main ( void )
{
	int n;
	while (cin >> n, n) {
		int p = 0;
		for (int i = 0; i < n; ++i) {
			LINT x;
			cin >> x;

			if (i == 0) { as[0] = x; }
			else {
				if (as[p] * x < 0) {
					as[++p] = x;
				}
				else {
					as[p] += x;
				}
			}
		}

		LINT ans = 0, sum = 0;
		for (int i = 0; i <= p; ++i) {
			sum = max(0LL, sum + as[i]);
			ans = max(ans, max(sum, as[i]));
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <string>
#include <queue>
#include <algorithm>
#include <cctype>
#define shosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const double pi=acos(-1);

ll n,ans;
vector<ll> dp,num;

int main(){
	while(1){
		cin>>n;
		if(!n) break;
		ans=-1;
		dp=vector<ll>(n);
		num=vector<ll>(n);
		for(int i=0;i<n;i++) cin>>num[i];
		dp[0]=num[0];
		for(int i=0;i<n-1;i++) dp[i+1]=max(num[i+1],num[i+1]+dp[i]);
		for(int i=0;i<n;i++) ans=max(ans,dp[i]);
		cout<<ans<<endl;
	}
}

C++: #define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

using namespace std;

int main()
{
	for (; ;)
	{
		int n;
		cin >> n;

		if (n == 0)
		{
			break;
		}

		vector<int> a(n);
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a.at(i));
		}

		int maxv = a.at(0);
		vector<int> sum;
		sum.push_back(0);
		for (int i = 0; i < n; i++)
		{
			int s = sum.back() + a.at(i);
			sum.push_back(s);


			for (int j = 0; j < sum.size() - 1; j++)
			{
				int x = sum.back() - sum.at(j);
				maxv = max(maxv, x);
			}
		}

		cout << maxv << endl;
	}
}

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

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

int main(){
	int n;
	int a;
	int sum;
	int sum2;
	long int max;
	cin >> n;
	while(n){
		cin >> sum;
		max = sum;
		for(int i=0;i<n-2;i++){
			cin >> sum2;
			sum += sum2;
			if(sum < sum2){
				sum = sum2;
			}
			if(sum > max){
				max = sum;
			}
		}
		cout << max << endl;
		cin >> n;
	}
	return 0;
}

C++: #include <stdio.h>

int main()
{
	int n, x, p;
	
	while(scanf("%d", &n) != 0){
		int a[n];

		if (n == 0) break;

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

		x = a[0];

		for(int i = 1; i <= n; i++){
			for(int j = 0; j < n-i+1; j++){
				p = 0;

				for(int k = 0; k < i; k++){
					p += a[j+k];
				}

				if(x < p) x = p;
			}
		}

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

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

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

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
ll a[5000];

ll sum(int i,int j){
	ll s=0;
	rep(k,j){
		s+=a[i+k];
	}
	return s;
}

int main(){
	while(cin>>n,n!=0){
		int c=0;
		ll MAX=-100000000;
		cin>>a[0];
		rep(i,n-1){
			int t;
			cin>>t;
			if(a[c]*t<0){
				a[++c]=t;
			}
			else a[c]+=t;
		}
		for(int i=1;i<=c+1;i++){
			rep(j,c+1-i){
				MAX=max(MAX,sum(j,i));
			}
		}
		cout<<MAX<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;

const int INF = 1000000000;
const int MOD = 1000000007;
const double EPS = 1e-8;

int main() {
	int n;
	while (cin >> n, n) {
		int a[n], ans = -10000000;
		rep (i, n) cin >> a[i];
		int dp[n];
		dp[0] = a[0];
		REP (i, 1, n) {
			// dp[i] = dp[i - 1] + a[i];
			dp[i] = max(dp[i - 1] + a[i], a[i]);
		}
		rep(i, n) {
			ans = max(ans, dp[i]);
		}
		cout << ans << endl;
	}
}