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 <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;

ll dp[5000];
int main(){
	int n,a;
	while(1){
		cin >> n;
		if(!n)break;
		ll ans=0;
		rep(i,n){cin>>a;
			if(i==0)dp[0]=a;
			else{
				if(dp[i-1]>0)dp[i]=dp[i-1]+a;
				else dp[i] = a;
			}
			ans = max(ans,dp[i]);
		}
		cout<<ans<<endl;
	}

}

C++: #include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
if(n==0)
{
	break;
}
else
{
int a[n];
int r[5000];
for(int h=0;h<5000;h++)
{
	r[h]=-100001;
}
for(int i=0;i<n;i++)
{
	cin>>a[i];
}
for(int j=0;j<n;j++)
{
	if(a[j]<0)
	{r[j]=a[j];}
	else
	{
		r[j]=a[j];
		for(int k=j+1;k<n;k++)
		{
			if(a[k]<0)
			{
				break;
			}
			else
			{
				r[j]+=a[k];	
			}
		}
		
	}
}
int l=0;
	while(r[0]!=-100001)
	{
		if(r[l]>r[0])
		{
			r[0]=r[l];
			l++;
		}
	}
}
return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long 
int main(){
    ll n;
    cin >> n;
    while(n!=0){
        vector<ll> vec(n);
        for(ll i=0;i<n;i++){
            cin >> vec.at(i);
        }
        vector<ll> rsw(n+1);
        rsw.at(0) = 0;
        for(int i=1;i<n+1;i++){
            rsw.at(i) = rsw.at(i-1) + vec.at(i-1);
        }
        ll ans = vec.at(0);
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                if(ans < rsw.at(j+1)-rsw.at(i)){
                    ans = rsw.at(j+1) - rsw.at(i);
                }
            }
        }
        cout << ans << endl;
        cin >> n;

    }
}

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

int main(){
	int n, max1, max2, a[5001];

	while( cin >> n , n ){
		for(int i=0 ; i<n ; i++){
			cin >> a[i] ;
		}
		max1 = a[0];

		for(int i=0 ; i<n ; i++){
			int sum = 0;
			for(int j=i ; j<n ; j++){
				sum += a[j];
				max1 = max( max1 , sum );
			}
		}
		cout << max1 << endl;
	}
}

C++: #include<algorithm>
#include<iostream>
#define INF -11111111
using namespace std;
int main(){
  int n;
  int a[5555]; // 配列
  while(1){
    int ret = INF,sum;
    cin >> n;
    if(!n)break;
    for(int i=0;i<n;i++){
      cin >> a[i];
    }
    for(int i=0;i<n;i++){
      sum = 0;
      for(int j=i;j<n;j++){
	sum += a[j];
	ret = max( ret , sum );
      }
    }
    cout << ret << endl;
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>
using namespace std;
typedef  long long int lint;
//typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
#define mp make_pair
#define P pair<int,int>

int main() {
	while (1) {
		int N;
		cin >> N;
		if (N == 0) {
			return 0;
		}
		vector<lint>A(N);
		rep(i, N) {
			cin >> A[i];
		}
		vector<lint>S(N);
		S[0] = A[0];
		for (int i = 1; i < N; i++) {
			S[i] = S[i - 1] + A[i];
		}
		reverse(S);
		S.push_back(0);
		reverse(S);
		lint ans = -10000000000;
		for (int i = 1; i < N + 1; i++) {
			for (int j = 0; j < N + 1; j++) {
				if (j < i) {
					ans = max(ans, S[i] - S[j]);
				}
			}
		}
		cout << ans << endl;
	}
}

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

int main(void)
{
    int n;
    int num[5001];
    int sum;
    int max;
    
    while (scanf("%d", &n)){
        if (n == 0) break;
        
        memset(num, 0, 5001);
        for (int i = 0; i < n; i++){
            scanf("%d", &num[i]);
        }
        max = num[0];
        
        for (int i = 0; i < n; i++){
            sum = 0;
            for (int j = i; j < n; j++){
                sum += num[j];
                if (max < sum) max = sum;
            }
        }
        
        printf("%d\n", max);
    }
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<long long int> hyou(n+1, 0);
		long long int a;
		long long int MAX = 0;
		int maxn = 0;
		for (int i = 0; i < n; i++) {
			cin >> a;
			hyou[i+1] = hyou[i] + a;
			if (i == 0) {
				MAX = hyou[1];
				maxn = 1;
			} else {
				if (hyou[i+1] > MAX) {
					MAX = hyou[i+1];
					maxn = i+1;
				}
			}
		}
		long long int MIN = 0;
		int minn = 0;
		for (int i = 1; i < maxn; i++) {
			if (hyou[i] < 0) {
				if (hyou[i] < MIN) {
					MIN = hyou[i];
					minn = i;
				}
			}
		}
		if (minn != 0) {
			MAX -= MIN;
		}
		cout << MAX << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int a[5000];
int b[5000][5000];
int main() {
	int n;
	while (cin >> n&&n != 0) {
		int sum = -100000;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			if (i == 0) {
				b[0][i] = a[i];
				if (sum < b[0][i])
					sum = b[0][i];
			}
			else {
				b[0][i] = b[0][i - 1] + a[i];
				if (sum < b[0][i])
					sum = b[0][i];
			}
		}
		
		for (int j = 1; j < n; j++) {
			for (int i = j; i < n ; i++) {
				b[j][i] = b[j - 1][i] - a[j - 1];
				if (sum < b[j][i])
					sum = b[j][i];
			}
		}
		cout << sum << endl;
	}
}