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 <climits>
using namespace std;
int s[2];
int main(){
  int n, a;
  while(cin >> n, n){
    int ans = -(INT_MAX-1);
    for(int i = 0 ; i < n ; i++){
      cin >> a;
      if(i == 0)s[0] = a;
      else{
	s[1] = max(a, a + s[0]);
	s[0] = s[1];
      }
      ans = max(ans, s[0]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cmath>
#define INF 1000000000
using namespace std;
int ary[5010],fn[80],ff[80],fl[80],fr[80];

int main(){
	int N;
	while(true){
		cin>>N;
		if(N==0) break;
		for(int i=0;i<N;i++){
			cin>>ary[i];
		}
		int NN=sqrt(N),NNN=(N-1)/NN+1,cnt,hog,hog2;
		for(int i=0;i<NNN;i++){
			hog=hog2=-INF;
			for(int j=NN*i;j<min(NN*(i+1),N);j++){
				for(int k=j;k<min(NN*(i+1),N);k++){
					cnt=0;
					for(int l=j;l<=k;l++){
						cnt+=ary[l];
					}
					hog=max(hog,cnt);
					if(k==min(N,NN*(i+1))-1) hog2=max(hog2,cnt);
					if(j==NN*i&&k==min(N,NN*(i+1))-1) ff[i]=cnt;
				}
				if(j==NN*i) fl[i]=hog;
			}
			fn[i]=hog;
			fr[i]=hog2;
		}
		int res=-INF;
		for(int i=0;i<NNN;i++){
			res=max(res,fn[i]);
			for(int j=i+1;j<NNN;j++){
				hog=0;
				cnt=fr[i]+fl[j];
				for(int k=i+1;k<j;k++){
					cnt+=ff[k];
				}
				res=max(res,cnt);
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

C++: #include <stdio.h>

int seqsum(int n){
	int a = 0;
	int b[5000];
	int c = 0;
	int s = true;
	scanf("%d", &a);
	b[0] = a;
	s = a > 0 ? 1 : -1;

	for(int i=1; i<n; ++i){
		scanf("%d", &a);
		if(s * a > 0){
			b[c] += a;
		}else{
			b[++c] = a;
			s = a > 0 ? 1 : -1;
		}
	}

	int m = b[0] < 0 ? 0 : b[0];
	int sum = m;
	for(int i=1; i<=c; ++i){
		if(sum < 0 && m < b[i]){
			sum = m = b[i];
		}else{
			sum += b[i];
			if(m < sum){
				m = sum;
			}
		}
	}
	return m;
}

int main(void){
	int n = 0;
	while(true){
		scanf("%d", &n);
		if(n == 0){ break;}
		printf("%d\n", seqsum(n));
	}
	return 0;
}

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

using namespace std;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<int> num(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> num[i];
		}
		vector<int> memo(n);
		memo[0] = num[0];
		for(int i = 1; i < n; ++i)
		{
			if(memo[i - 1] + num[i] > num[i])
			{
				memo[i] = memo[i - 1] + num[i];
			}
			else
			{
				memo[i] = num[i];
			}
		}
		int max_num = -999999999;
		for(int i = 0; i < n; ++i)
		{
			if(max_num < memo[i])
			{
				max_num = memo[i];
			}
		}
		cout << max_num << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,math[5010],most,bell;
  while(1){
    cin >> n;
    if(n==0){
      break;
    }


    for(int i=0;i<n;i++){
      cin >> math[i];
    }
    most = math[0];
    for(int i=0;i<n;i++){
      bell=0;
      for(int j=i;j<n;j++){
	bell+=math[j];
	if(most<bell){
	  most=bell;
	}
      }
    }
    cout << most << endl;
  }
}

C++: #include <stdio.h>
#define MAX(x,y) x>y?x:y

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

    int a[5000];
    int max = 0;

    int i,j,k;
    for(i=0; i<n; ++i){
      scanf("%d", &a[i]);
      for(j=0; j<=i; ++j){
        int sum = 0;
        for(k=j; k<=i; ++k){
          sum += a[k];
        }
        if(max<sum){
          max = sum;
        }
      }
    }

    printf("%d\n", max);
  }

  return 0;
}

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

int main(){
  int n;
  while(1){
    cin>>n;
    if(n==0) break;
    int i,cr,max;
    cr=0;
    max=-100000;
    while(n--){
      cin>>i;
      cr +=i;
      if(cr>max) max=cr;
      if(cr<0) cr=0;
    }
    cout<<max<<endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    int ans=0,seq=0;
    while(n--){
      int i;
      cin>>i;
      seq=max(seq+i,i);
      ans=max(ans,seq);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,i,j;
  cin >> n;
  while(n!=0) {
    long long sum[5001]={};
    int b;
    long long o=1<<20;
    o*=-1;
    cin >> sum[1];
    for(i=2;i<=n;i++){
      cin >> b;
      sum[i]=sum[i-1]+b;
      //cout << sum[i] << endl;
    }
    
    for(i=0;i<=n;i++){
      for(j=i+1;j<=n;j++){
	o=max(o,sum[j]-sum[i]);
      }
    }
    cout << o << endl;
    cin >> n;
  }
  return 0;
  
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
	int n;
	while(cin >> n && n)
	{

		vint v(n+1); v[0] = 0;
		rep(i, n)
			cin >> v[i+1];

		partial_sum(ALL(v), v.begin());

		int res = 0;

		rep(i, n) REP(j, i + 1, n + 1)
			chmax(res, v[j] - v[i]);

		cout << res << endl;
	}
}

C++: //#define _USE_MATH_DEFINES
//#include <cmath>
#include <iostream>
//#include <stdio.h>
//#include <stdlib.h>
//#include <iomanip>
#include <vector>
//#include <string>
//#include <algorithm>
//#include <functional>
//#include <time.h>
//#include <sstream>
//#include <queue>

using namespace std;


int main(){

	int n, buf;
	long long ans, sum;
	vector <int> p;


	while (1){
		p.clear();
		sum = 0;
		ans = -100000;

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

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

		for (int left = 0; left < n; left++){
			if (p[left] > 0){
				for (int right = left; right < n; right++){
					if (p[right] > 0){
						for (int i = left; i <= right; i++){
							sum += p[i];
						}
						if (sum > ans) ans = sum;
						sum = 0;
					}
				}
			}
		}

		cout << ans << endl;

	}

	return 0;
}

C++: /* AOJ 0022*/

#include<iostream>
#include<queue>
using namespace std;

int m;
int n;

void calc(int *data,int num,int sum){
	if(sum>=m){
		m=sum;
	}
	if(num==n){
		return;
	}
	else{
		calc(data,num+1,sum+data[num]);
	}
}

int main(){

	queue<int> ans;

	while(cin>>n){
		if(n==0){
			break;
		}
		int *data = new int[n];
		m=0;
		for(int i=0;i<n;i++){
			cin>>data[i];
		}

		for(int i=0;i<n;i++){
			calc(data,i,0);
		}
		ans.push(m);
	}

	int size=ans.size();
	for(int i=0;i<size;i++){
		cout<<ans.front()<<endl;
		ans.pop();
	}

	return 0;
}