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>
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;
	
	}

	return 0;

}

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

using namespace std;

#define N 2 << 20;

int main(){

  int n;


  while(cin >> n){
    if(n == 0) break;
    vector<int> V;
    int status;
    int in;
    int sum = 0;
    for(int i = 0; i < n; i++){
      cin >> in;
      if(i == 0){
	if(in < 0) status = -1;
	else if(in > 0)status = 1;
	else{
	  V.push_back(0);
	  status = 1;
	}


      } else {
	/*if(in == 0) {
	  V.push_back(sum);
	  sum = 0;
	  V.push_back(0);
	  }*/

	if(in < 0 && status == 1 /*|| in < 0 && status == 0*/){
	  status = -1;
	  V.push_back(sum);
	  sum = 0;
	}else if(in >= 0 && status == -1 /*|| in > 0 && status == 0*/){
	  status = 1;
	  V.push_back(sum);
	  sum = 0;
	}
      }
      sum += in;
    }

    V.push_back(sum);

    int max = -1*N;
    
    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 <iostream>
#include <vector>
using namespace std;

int main(){
	int n, k;
	vector<int> dp;
	while(true){
		cin >> n;
		if(n == 0)
			return 0;
		for(int i = 0; i < n; i++){
			cin >> k;
			if(i == 0)
				dp.push_back(k);
			else
				dp.push_back(max(dp[i -1] + k, k));
		}
		int max = dp[0];
		for(int i = 0; i < n; i++){
			if(max < dp[i]){
				max = dp[i];
			}
		}
		cout << max << endl;
	}
}

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

using namespace std;

int main(){
  vector<int> sum;
  int n;
  int input;

  while(cin>>n){
    if(n==0)break;
    vector<int> array;
    for(int i=0;i<n;i++){
      cin>>input;
      array.push_back(input);
    }
    vector<int> now;
    for(int i=0;i<n;i++){
      int a=0;
      for(int j=0;j<=i;j++){
	a+=array[j];
      }
      now.push_back(a);
    }
    int ma=0;
    for(int i=0;i<n;i++){
      if(i<n-1){
	ma=max(now[i],now[i+1]);
      }
    }
    sum.push_back(ma);
  }
  vector<int> ans;
  int i=0;
  int size=sum.size();
  while(!sum.empty()){
    ans.push_back(sum[size-1-i]);
    sum.pop_back();
    i++;
  }
  i=0;
  while(!ans.empty()){
    cout<<ans[size-1-i]<<"\n";
    ans.pop_back();
    i++;
  }
  return 0;
}

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

int main(){
	int n;
	while(cin >> n, n){
		//n++;
		vector<int> dp(n), vec(n);
		//t[0] = 0;
		/*for(int i = 1;i < n;i++){
			cin >> v[i];
			t[i] = max(t[i-1]+v[i], v[i]);
		}*/
		for(int i=0;i<n;i++)cin >> vec[i];
		dp[0]=vec[0];
		for(int i=1;i<n;i++)dp[i]=max(dp[i-1]+vec[i],vec[i]);
		cout << *max_element(dp.begin(), dp.end()) << endl;
	}
	return 0; 
}

/*
7
-5
-1
6
4
9
-6
-7
*/

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

int a[6000];
int n,ans;

void saiki(int num,int sum);

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

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

        for(int i = 0;i < n;i++) saiki(i,0);

        printf("%d\n",ans);

    }
    return 0;
}

void saiki(int num,int sum)
{
    if(num + 1 > n) return;
    sum += a[num];
    if(sum > ans) ans = sum;
    saiki(num+1,sum);
}

C++: #include<iostream>
#include<string.h>

int n;
int a[1000];

void add( int i, int x )
{	
	while( i <= n ){
		a[i] += x;
		i += i & (-i);
	}
	
	return;
}

int sum( int i )
{
	if( i == 0 ) return 0;

	int s = 0;
	while( i > 0 ){
		s += a[i];
		i -= i & (-i);
	}
	
	return s;
}

int main()
{
	while( 1 ){
		memset( a, 0, sizeof( a ) );
	
		std::cin >> n;
		if( !n ) break;
		
		for( int i = 1; i <= n; i++ ){
		 	int x;
			std::cin >> x;
			add( i, x );
		}
		
		int ans = 0;
		
		for( int i = 0; i < n; i++ ){
		 	for( int j = i + 1; j <= n; j++ ){
				ans = std::max( ans, sum( j ) - sum( i - 1 ) );
			}
		}
		
		std::cout << ans << std::endl;
	}
	
 	return 0;
}

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

int main() {
	int n, a, zip = 0;
	vector<int> an;
	vector<vector<int>> AN;
	while (cin >> n) {
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> a;
			an.push_back(a);
		}
		AN.push_back(an); an.clear();
	}

	for (int i = 0; i < AN.size(); i++) {
		int max = 0, MAX = 0 , counter = 0;
		for (int j = 0; j < AN[i].size(); j++) {
			if (AN[i][j] >= 0) {
				max += AN[i][j];
				if (MAX < max)MAX = max;
				counter = 1;
			}
			else {
				if (counter==1){
					if (AN[i][j] + max >= 0)max += AN[i][j];
					else {
						if (MAX < max)MAX = max;
						max = 0;
						counter = 0;
					}
				}
			}
		}
		cout << MAX << endl;
	}
	
	return 0;
}