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>

using namespace std;

int main() {
    int n = 0;
    while (cin>>n && n != 0) {
        int a[5001] = {};
        cin>>a[0];
        int max = a[0];
        for (int i = 1; i < n; i++) {
            cin>>a[i];
            if (a[i] >= 0) {
                int temp = 0;
                for (int s = i; s >= 0; s--) {
                    temp += a[s];
                    if (temp > max) max = temp;
                    if (temp < 0) break;
                }
            }
        }
        cout<<max<<endl;
    }
}

C++: #include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define HOGE 6000
#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define sz size()

const double EPS = 1e-10;
const int INF = 100000000;

int main(){
    int n;
    while(cin>>n){
        int ma=-INF;
        int dp[HOGE][HOGE];
        int data[HOGE];
        if(n==0)break;
        rep(i,n){
            cin>>data[i];
        }
        rep(i,n)dp[i][i]=data[i];
        for(int i=0;i<n-1;i++){
            for(int j=i+1;j<n;j++){
                dp[i][j]=dp[i][j-1]+data[j];
                ma=max(ma,dp[i][j]);
            }
        }
        cout<<ma<<endl;
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

int main() {

	int n;
	int a[5000];

	while(cin >> n) {
		if(n==0)
			break;
		for(int i=0; i<n; i++) {
			cin >> a[i];
		}
		int max=a[0];
		for(int i=0; i<n; i++) {
			int t=0;
			for(int j=i; j<n; j++) {
				t+=a[j];
				if(t>max)
					max=t;
			}
		}
		cout << max << endl;
	}

	return 0;

}

C++: // class point	の練習（クラスの高度な実装）
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <vector>
#include <queue>


typedef long long llong;
using namespace std;

int main() {
	int n;
	llong maxim;
	int input_num;
	int stock_sum;
	int reserve_max_neg;
	while (1) {
		cin >> n;
		if (!n)break;
		maxim = -100010;
		stock_sum = 0;
		for (int i = 0; i < n; i++) {

			cin >> input_num;
			stock_sum += input_num;
			if (stock_sum > maxim)maxim = stock_sum;
			if (stock_sum < 0)stock_sum = 0;
		}
		cout << maxim << endl;
	}
	return 0;
}

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

int main(){
    int a[5000], n, i, j;
    while (cin >> n && 1 <= n && n <= 5000){
        int maximum = -100000;
        for(i = 0; i < n; i++){
            cin >> a[i];
         }
        for(i = 0; i < n; i++){
            int sum = 0;
            for (j = i; j < n; j++){
                sum += a[j];
                maximum = max(maximum, sum);
            }
        }
        cout << maximum << endl;
    }
}

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

int main(){
	int n;
	int a;
	int sum;
	int sum2;
	int max;
	cin >> n;
	while(n){
		cin >> sum;
		max = -100001;
		for(int i=0;i<n-1;i++){
			cin >> sum2;
			sum += sum2;
			if(sum < sum2){
				sum = sum2;
			}
			if(sum > max){
				max = sum;
			}
		}
		cout << max << endl;
		cin >> n;
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

int main(){
	int n;
	while(cin>>n,n){
		int a[n];
		long long int max=0;
		for(int i=0;i<n;i++){
			a[i]=0;
			int input;
			cin>>input;
			for(int j=0;j<=i;j++){
				a[j]+=input;
				if(max<a[j])max=a[j];
			}
		}
		cout<<max<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<climits>
using namespace std;
#define REP(i,a,n) for(int i = a ; i < n ; i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int t[5002];

int main(){
  int n;

  while(cin>>n,n){
    rep(i,n){
      cin>>t[i];
    }

    int ans = INT_MIN;
    rep(i,n){
      int sum = 0;
      REP(j,i,n){
        sum += t[j];
        if(ans < sum){
          ans = sum;
        }
      }
    }

    cout<<ans<<endl;
  }
}

C++: #include <iostream>
using namespace std;
int main(){
    int n,sum,t,i;
    while(cin>>n&&n){
        sum=0;
        for(i=0;i<n;i++){cin>>t;sum+=t;}
        cout<<sum<<endl;
    }
}

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

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

C++: #include <bits/stdc++.h>
using namespace std;

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

template <class Monoid> struct SegmentTree{
    using Func = function<Monoid(Monoid, Monoid)>;

    int size;
    vector<Monoid> seg;
    const Func merge;
    const Monoid def;

    SegmentTree(int n, Monoid d, const Func f): merge(f), def(d){
        for(size = 1; size <= n; size <<= 1);
        seg.assign(size*2, def);
    }

    Monoid& operator[] (int i) { return seg[i+size]; };
    void build(){ for(int i = size-1;i >= 0;i--) seg[i] = merge(seg[i*2], seg[i*2+1]); } 

    void update(int i, Monoid val){
        seg[i += size] = val;
        while(i >>= 1) seg[i] = merge(seg[i*2], seg[i*2+1]);
    }

    void add(int i, Monoid val){ update(i, val+seg[i+size]); }

    Monoid get(int a, int b){
        Monoid l = def, r = def;
        for(a += size, b += size; a < b; a >>= 1, b >>= 1){
            if(a & 1) l = merge(l, seg[a++]);
            if(b & 1) r = merge(seg[--b], r);
        }
        return merge(l, r);
    }
};

int solve(int n);

struct Data{
	int l;
	int r;
	int sum;
	int ma;
	bool t;
	Data(): l(0), r(0), sum(0), ma(0), t(true){}
	Data(int a){
		l = a;
		r = a;
		sum = a;
		ma = a;
		t = false;
	}

};


signed main(){
	int n;

	while(cin >> n, n != 0){
		solve(n);
	}

	return 0;
}

int solve(int n){

	auto merge = [](Data a, Data b){
		if(b.t) return a;
		if(a.t) return b;
		Data c;
		c.l = max(a.l, a.sum + b.l);
		c.r = max(b.r, b.sum + a.r);
		c.sum = a.sum + b.sum;
		c.ma = max({a.ma, b.ma, a.r + b.l});
		c.t = false;
		return c;
	};

	SegmentTree<Data> seg(n, Data(), merge);

	for(int i = 0;i < n;i++){
		int a;
		cin >> a;
		seg[i] = Data(a);
	}
	seg.build();
	cout << seg.get(0, n).ma << endl; 

	return 0;
}