Go: package main

import "fmt"

func main() {
	var result []int
	for {
		var n, a, maxSum int
		fmt.Scan(&n)

		if n == 0 {
			break
		}

		maxSum = 0
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			if i == 0 {
				sum = a
				maxSum = sum
			} else if sum+a < a {
				sum = a
				if maxSum < sum {
					maxSum = sum
				}
			} else {
				sum = sum + a
				if maxSum < sum {
					maxSum = sum
				}
			}
		}

		result = append(result, maxSum)
	}

	for _, v := range result {
		fmt.Println(v)
	}
}

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

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

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

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

    for (int i = 1; i < seq.size(); ++i) {
      if (seq[i-1] > 0)
	seq[i] += seq[i-1];
    }

    cout << *max_element(seq.begin(), seq.end()) << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int main()
{
	int num;

	while( cin >> num ) {
		if (num == 0 )
			break;

		int tmpnum = 0;
		int currnum;
		int maxnum = 0;
		for( int i = 0; i < num; i++ ) {
			cin >> currnum;
			if( currnum + tmpnum > maxnum ) {
				maxnum = currnum + tmpnum;
			}
			
			if( currnum + tmpnum > 0 )
				tmpnum += currnum;
			else
				tmpnum = 0;
		}
		cout << maxnum << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int a[5000],i,j,sum=0,max[5000]={-10001},c=0,n;
	
	while(1){
		scanf("%d",&n);
		if( n == 0)
			break;
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		for(i=0;i<n;i++){
			for(j=i;j<n;j++){
				sum = sum + a[j];
				if(max[c] < sum)
					max[c] = sum;
			}
			sum = 0;
		}
		c++;
	}
	for(i=0;i<c;i++){
		printf("%d\n",max[i]);
	}
	return 0;
}

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

using namespace std;

const int MAX_N = 5000;

int as[MAX_N+1];

int main ( void )
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	while (cin >> n, n) {
		as[0] = 0;
		for (int i = 0; i < n; ++i) {
			int x;
			cin >> x;
			as[i+1] = as[i] + x;
		}

		int ans = -(1 << 29);
		for (int i = 0; i < n; ++i) {
			for (int j = i+1; j <= n; ++j) {
				ans = max(ans, as[j] - as[i]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}