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>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <climits>
#include <utility>

#define PRINT(STR) cout << STR << endl;
#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 ALL(V) (V).begin(), (V).end()

typedef long long ll;

using namespace std;

int nextInt() { int n = -1; cin >> n; return n; }
double nextDouble() { double d = -1; cin >> d; return d; }
ll nextll() { ll n = -1; cin >> n; return n; }
string nextString() { string str; cin >> str; return str; }

int INF = 1LL << 25;
double EPS = 1e-9;

// BEGIN //////////////////////////////////////////////////////////////

int main() {
	while (true) {
		int N = nextInt();
		if (N == 0) break;
		int score_max = -INF, score = 0;
		REP(i, N) {
			int d = nextInt();
			score += d;
			score_max = max(score, score_max);
			score = max(score, 0);
		}
		PRINT(score_max);
	}

	return 0;
}

// END   //////////////////////////////////////////////////////////////

C++: #include <stdio.h>
int arr[] = int[5000];
int sub[] = int[5001];
int main() {
  while(1) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      return 0;
    }
    for(int i = 0; i < n; ++i) {
      scanf("%d", &arr[i]);
    }
    sub[0] = 0;
    for(int i = 1; i < n+1; ++i) {
      sub[i] = sub[i-1] + arr[i-1];
    }
    for(int i = 0; i < n; ++i) { 
        for(int j = i + 1; j < n + 1; ++j) {
          m = max(m, sub[j] - arr[i]);
        }
    }
    printf("%d", m);
  }
}

C++: #include<stdio.h>

int main(){
	long int n,a[5000],b[5000],i,j,max;
	
	while(0<=scanf("%ld",&n)){
		if(n==0)break;
		max=-2147483647;
		for(i=0;i<n;i++){
			scanf("%ld",&a[i]);
			if(max<a[i])max=a[i];
		}
		b[0]=a[0];
		for(i=1;i<n;i++){
			b[i]=b[i-1]+a[i];
			if(b[i]>max)max=b[i];
		}
		
		for(i=1;i<n;i++){
			for(j=i;j<n;j++){
				if(max<b[j]-b[i-1])max=b[j]-b[i-1];
			}
		}
		
		printf("%ld\n",max);
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main(){
	int n;
	int A[5000];
	int sum=0,max=0;
	scanf("%d",&n);
	while(n != 0){
			memset(A,0,sizeof(A));
			for(int i=0;i<n;i++)cin >> A[i];

			for(int i=0;i<n;i++){
				int next = i + 1;
				sum = A[i];
				if(sum > max)max = sum;
				while(next < n){
					sum += A[next];
					if(sum > max)max = sum;

					next++;
				}
			}

			cout << max << endl;
			max = 0;
			scanf("%d",&n);	
	}


}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <cctype>
#include <cstdlib>
using namespace std;

#define INF (1 << 20)
#define EPS (1e-10)
#define EQ(a,b) (fabs((a)-(b)) < EPS)

int main(){
	int n;
	while (cin >> n && n != 0) {
		int ret = -INF;
		int a[5000];
		for (int i = 0; i < n; ++i) cin >> a[i];
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				int sum = 0;
				for (int k = i; k <= j; ++k) sum += a[k];
				ret = max(ret, sum);
			}
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(){
	int n,max=0;
	while(cin>>n&&n!=0){
		int a[5000]={0};
		for(int x=0;x<n;x++){
			cin>>a[x];
			for(int i=1;i<=n;i++){
				for(int j=0;j<n;j++){
					int sum=0;
					for(int k=j;k<i;k++){
						sum+=a[k];
					}
					if(max<sum)max=sum;
				}
			}
		}
		cout<<max<<endl;
			max=0;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
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{
					++p;
					A[p] = k;
					isPlus = !isPlus;

					while (1 < p && isPlus){
						if (m < A[p - 2]) m = A[p - 2];
						if (m < A[p]) m = A[p];
						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;
					}
					if (m < A[p]) m = A[p];
				}
			}
		}
		if (m < A[p]) m = A[p];
		cout << m << "\n";
	}
	return 0;
}

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

// warning: include *e
int getBackMaxSum(int* s, int *e) {
	int sum = 0, maxSum = 0x80000000;
	for (; s <= e; e--) {
		sum += *e;
		maxSum = max(sum, maxSum);
	}
	return maxSum;
}

int main () {
	while (cin >> n, n) {
		int a[n], maxSum = 0x80000000;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			maxSum = max(maxSum, getBackMaxSum(a, a + i));
			assert(a[i] <= maxSum);
		}
		cout << maxSum << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> a(n, 0); //?´??????¨
    vector<int> b(n, 0); //????????¨
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int k = n - 1;
    while (a[k] < 0)
      k--;
    b[k] = a[k];
    for (int i = k - 1; i > -1; i--) {
      if (a[i] < 0)
	continue;
      else {
	int j = i + 1;
	int s = 0; //?????????????????????????????¨
	while (a[j] < 0) {
	  s += a[j];
	  j++;
	}
	//cout << "s = " << s << " " << "i = " << i << " " << "j = " << j << endl;
	if (s + b[j] >= 0)
	  b[i] += s + b[j] + a[i];
	else // if (a[i] > s + b[j])
	  b[i] += a[i];
	//cout << "b[" << i << "]= " << b[i] << endl;
      }
    }
    /*
    for (int i = 0; i < n; i++)
      cout << "b[" << i << "]= " << b[i] << endl;
    */
    int max = b[0];
    for (int i = 1; i < n; i++)
      if (max < b[i])
	max = b[i];
    cout << max << endl;
  }
}

C++: #include <iostream>

using namespace std;

int main(void){
	int n;
	long long int a[5000]={0};
	while(cin >> n){
		long long int max = 0;
		if(n == 0) break;
		for(int i=0;i<n;i++){
			cin >> a[i];
		}
		for(int i=0;i<n;i++){
			long long int sum = 0;
			for(int j=0;i+j<n;j++){
				sum += a[i+j];
				if(max < sum) max = sum;
			}
		}
		cout << max << "\n";
	}
	return 0;
}

C++: //まともに使い方を理解していないくせにincludeしまくる人間のクズ
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>


//using namespace stdしないと表記がめんどくさいからね、仕方ないね
using namespace std;

//可読性を上げるためのdefineが仇となる典型的パターン
#define rep(n) for(int i = 0; i < x; i++)

//ローカル変数とグローバル変数は使い分けよう！
stack<short> x, y;
unsigned long int ans, array[5000];
short i, n;
//関数って楽しいよね！（魔王スマイル）

void solve(unsigned long int sum, short i){
    if(ans < sum) ans = sum;
    if(i == n) return;
    solve(sum + array[i], i + 1);
    solve(0, i + 1);
}

int main(){
    while(true){
        scanf("%lld", &n);
        if(n == 0) return 0;
        ans = 0;
        for(i = 0; i < n; i++) scanf("%lld", &array[i]);
        solve(0, 0);
        printf("%lld\n", ans);
    }
}