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++: // start 20:09
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

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

using namespace std;

/*
メモ化
[開始インデックス][終了インデックス] = 合計値
*/

vector<vector<int> > memo(5000, vector<int>(5000, 0));

int sum(vector<int> &nums, int oft, int len) {
    int ret = 0;
    for ( int i = oft; i < len; i++ ) {
        // if ( memo[oft+i][oft+len] ) {
        //     return ret + memo[oft+i][oft+len];
        // }
        ret += nums[oft+i];
        // memo[oft+i][oft+len] = ret;
    }
    return ret;
}

int main() {
    int n;
    
    while(cin >> n, n) {
        int max_n = 0;
        vector<int> nums(n);

        REP(i, n) cin >> nums[i];
        
        REP(oft, n) REP(len, n-oft) {
            max_n = max(max_n, sum(nums, oft, len));
        }
        cout << max_n << endl;
    }

    return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define MOD 1000000007
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REP1(i, N) for (int i = 1; i <= N; ++i)
#define RREP(i, N) for (int i = N - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()

int main() {
  int n;
  while (cin >> n) {
    if (n == 0) break;
    int a[n];
    REP(i, n) cin >> a[i];
    int sum[n + 1];
    sum[0] = 0;
    REP(i, n) sum[i + 1] = sum[i] + a[i];
    int ans = -1e9;
    REP1(i, n) REP(j, i) ans = max(ans, sum[i] - sum[j]);
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main() {
	int a,i,j,k,n,max,sum;
	vector<int> s;
	while (cin >> n) {
		if (n == 0)break;
		s.clear();
		max = 0;
		for (i = 0; i < n; i++) {
			cin >> a;
			s.push_back(a);
		}
		for (j = 0; j < n; j++) {
			sum = 0;
			for (k = j; k < n; k++) {
				sum += s[k];
				if (sum > max)max = sum;
			}
		}
		cout << max << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <climits>
using namespace std;
#define rep2(i,from,to) for(int i=(from); i<(to); ++(i))
#define rep(i,to) rep2(i,0,to)
int main() {
	int n;
	while(cin >> n && n) {
		int a[5001];
		int Max = INT_MIN;
		rep(i,n) {
			cin >> a[i]; 
		}
		rep(i,n) {
			int sum = 0;
			sum += a[i];
			rep2(j,i+1,n) {
				sum += a[j];
				Max = max(Max, sum);
			}
		}
		cout << Max << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int Merge(vector<int>& A, int p)
{
	while (p > 1 && A[p] > 0){
		int a = A[p - 2] + A[p - 1] + A[p];
		if ((a >= A[p - 2]) && (a >= A[p])){
			A[p - 2] += A[p - 1] + A[p];
			A[p - 1] = A[p] = 0;
			p -= 2;
		}
		else break;
	}
	return p;
}
int main()
{
	int n, k, p, m;
	bool isPlus = true;
	while (cin >> n){
		if (0 == n) break;
		vector<int> A(n, 0);
		m = INT_MIN;
		p = 0;
		for (int i = 0; i < n; ++i){
			cin >> k;
			if (m < k) m = k;
			if (0 == i){
				A[p] = k;
				isPlus = (k > 0);
			}
			else {
				if (isPlus == (k > 0)){
					A[p] += k;
				}
				else{
					if (isPlus){
						p = Merge(A, p);
						if (m < A[p]) m = A[p];
					}
					++p;
					A[p] = k;
					isPlus = !isPlus;
				}
			}
		}
		p = Merge(A, p);
		if (m < A[p]) m = A[p];
		cout << m << "\n";
	}
	return 0;
}

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

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

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
    while(1){
        ll n; cin >> n;
        if(!n) break;
        vl a(n); rep(i,n) cin >> a[i];
        ll s = 0;
        ll ans = -linf;
        rep(i,n){
            s = max(s + a[i], a[i]);
            chmax(ans, s);
        }
        cout << ans << endl;
    }
}

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;
					}
					if (max < a[a.size() - 1])
					{
						max = a[a.size() - 1];
					}
					if (max < a[a.size() - 2])
					{
						max = a[a.size() - 2];
					}
				}
			}
		}
		std::cout << max << std::endl;
	}
	return 0;
}