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<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int main(void){
	ll ans[10000];
	ll N=0;
	while(true){
		int n;
		cin>>n;
		if(n==0)break;
		ll a[5000];
		// ll b[5000];
		// int N=0;
		// bool plus=true;
		// rep(i,n){//????????£?¶???????????¬?????????¨???????????¨??????
		// 	cin>>a[i];
		// 	if(i==0){
		// 		if(a[0]<0)plus=false;
		// 	}
		// 	if((plus && a[i]<0)||(!plus && a[i]>=0)){//?¬????????????????
		// 		N+=1;
		// 	}
		// 	b[N]+=a[i];
		// }

		rep(i,n){//????????£?¶???????????¬?????????¨???????????¨??????
			cin>>a[i];
		}
		ll max=0;
		ll current;
		rep(i,n){
			current=0;
			reg(j,i,n-1){
				current+=a[j];
				if(current>max)max=current;
			}
		}
		ans[N]=max;
		N++;
	}
	rep(i,N)cout<<ans[i]<<endl;
	return 0;
}

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

int main()
{

	int n, num;
	vector< long int > a;

	while( 1 ) {

		int Max = -1000001;

		cin >> n;
		if( n == 0 ) break;

		for( int i = 0; i < n; i++ ) {
		
			cin >> num;
			a.push_back( num );
			
		}

		//?????§???
		for( int i = 1; i < n; i++ ) {
		
			a[i - 1] + a[i] > a[i] ? a[i] = a[i - 1] + a[i] : a[i] = a[i];

			if ( a[i] > Max ) {
				Max = a[i];
			}
		
		}

		cout << Max << endl;

		a.clear();
	
	}

	return 0;

}
*/

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{

	int n, num;
	vector< int > a;

	while( 1 ) {
	
		cin >> n;
		if( n == 0 ) break;

		int Max = -100001;

		for( int i = 0; i < n; i++ ) {

			cin >> num;
			a.push_back( num );

		}

		for( int i = 1; i < n; i++ ) {

			a[i] = max( a[i], a[i] + a[i - 1] );
			Max  = max( Max, a[i] );
		
		}

		cout << Max << endl;

		a.clear();
	
	}

	return 0;

}

C++: #include <stdio.h>

int main() {
        int n, a;

        while (true) {
                scanf("%d", &n);
                if (!n) {
                        break;
                }

                int sum = 0, max = 0;
                for (int i = 0; i < n; i++) {
                        scanf("%d", &a);
                        sum += a;
                        if (sum < 0) {
                                sum = 0;
                                continue;
                        }
                        if (sum > max) {
                                max = sum;
                        }
                }
                printf("%d\n", max);
        }

        return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int sum(int a[],int st,int num){
  int s =0;
  for(int i = 0;i<num;i++){
    s = s + a[i+st];
  }
  return s;
}
    
int main(){
  int n; int const N = 5000;
  while(cin >> n){
    if(n == 0){return 0;}
    int  a[N],s,num,st,tmp=0;
    for(int i=0;i<n;i++)
      cin >> a[i];
    for(num = 1;num<=n;num++){
      for(st=0;st<n;st++){
	s = sum(a,st,num);
	if(s>tmp){swap(s,tmp);}
      }
    }
    cout << tmp<<endl;
  }
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        int plus[5000] = {0}, max = 2 >> 24, input;
        rep(i,n){
            cin >> input;
            plus[i]+= input;
            plus[i+1] = plus[i];
            if(max < plus[i]){
                max = plus[i];
            }
        }
        rep(i,n){
            rep(j,i-1){
                if(max < plus[i] - plus[j]){
                    max = plus[i] - plus[j];
                }
            }
        }
        cout << max << endl;
    }
}

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

bool Positive(int x)
{
	return x>=0;
}

int CalcMax( const std::vector<int>& v )
{
	int max = 5000*(-100000)-1;//考えられる最小値

	for( int i0=0 ; i0<v.size(); ++i0 )
	{
		for( int i1 = v.size()-1; i0<=i1; --i1 )
		{
			int sum = std::accumulate( v.begin()+i0, v.begin()+i1+1, 0 );
			if( max < sum )
			{
				max = sum;
			}
		}
	}
	return max;
}

int main()
{
	int n;
	while( std::cin >> n, n )
	{
		std::vector<int> v;
		for( int i=0; i<n; ++i )
		{
			int a;
			std::cin >> a;
			if(i==0)
			{
				v.push_back(a);
				continue;
			}

			if( Positive(v[v.size()-1]) == Positive(a) )
			{
				v[v.size()-1] += a;
			}
			else
			{
				v.push_back(a);
			}
		}

		std::cout << CalcMax(v) << '\n';
	}
}

C++: #include <iostream>

#include <vector>

#include <algorithm>

#include <sstream>

#include <string>

#include <cmath>

#include <list>

#include <set>
#include <cstdlib>
using namespace std;

int in() {int x; cin >> x; return x;}

template <typename T> class SegTree

{

public:

	int n;

	T T_max;

	vector<T> dat;

	SegTree(int n_, T t_max)

	{

		int n = 1;

		T_max = t_max;

		while (n < n_)n *= 2;

		for (int i = 0; i < 2 * n - 1; i++)dat.push_back(t_max);

	}

	void update(int k, T a)

	{

		k += n - 1;

		dat[k] = a;

		while (k > 0)

		{

			k = (k - 1) / 2;

			dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);

		}

	}



	//[a,b)

	T query(int a, int b)

	{

		return _query(a, b, 0, 0, n);

	}



private:

	T _query(int a, int b, int k, int l, int r)

	{

		if (r <= a || b <= l)return T_max;

		if (a <= l && r <= b)return dat[k];

		else

		{

			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);

			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);

			return min(vr, vl);

		}

	}

};


int main()
{
	int n;
	while (cin >> n && n)
	{
		vector<long long> a; a.push_back(0);
		long long ans = -999999999;
		for (long long i = 0; i < n; i++)
		{
			a.push_back(in());
		}
		for (long long i = 1; i < n + 1; i++)
		{
			a[i] += a[i - 1];
		}

		for (long long l = 0; l < n + 1; l++)
		{
			for (long long r = l; r < n + 1; r++)
			{
				ans = max(ans, a[r] - a[l]);
			}
		}
		cout << ans << endl;


	}
}

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

vector<int> num;

int main(void){
  while(true){
    int maximum = 0, n;
    cin >> n;
    if(!n)  break;
    num.clear();
    for(int i = 0; i < n; i++){
      int tmp;
      cin >> tmp;
      num.push_back(tmp);
    }
    for(int i = 0; i < num.size(); i++){
      int sum = num[i];
      for(int j = i + 1; j < num.size(); j++){
        sum += num[j];
        if(sum > maximum) maximum = sum;
      }
    }
    cout << maximum << endl;
  }
  return 0;
}