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 <vector>
#include <math.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;



int main()
{
	int n;
	while (cin >> n && n > 0)
	{
		vector<int> a = vector<int>(n);
		for (int i = 0; i < n; i++)
			cin >> a[i];

		vector<int> s = vector<int>(1, 0);
		bool f = true;
		for (int i = 0; i < n; i++)
		{
			if (a[i] >= 0)
			{
				if (!f)
					f = true;
				s[0] += a[i];
			}
			else if(s[0] > a[i])
			{
				if (f)
				{
					s.push_back(s[0]);
					f = false;
				}
				s[0] += a[i];
			}
			else
			{
				s[0] = 0;
			}

		}
		for (int i = 1; i < s.size(); i++)
		{
			if (s[i] > s[0])
				s[0] = s[i];
		}
		cout << s[0] << endl;
	}

	return 0;
}

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

int main() {
	int n;
	while (cin >> n, n) {
		int k[5003];
		for (int i = 0;i < n;i++)cin >> k[i];

		int dp[5002];
		dp[0] = 0;
		for (int i = 1;i <= n;i++) {
			dp[i] = max(dp[i - 1] + k[i - 1], k[i - 1]);
		}

		int ans = 1e-10;
		for (int i = 1;i <= n;i++)ans = max(ans, dp[i]);
		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n),n){
        int stage[100000] = {0};
        for(int i = 1;i <= n; i++){
            scanf("%d",&stage[i]);
        }
        int dp[100000]={0};
        dp[1] = stage[1];
        dp[2] = max(stage[2],dp[1]+stage[2]);
        int x = 0;
        int ans=dp[2];
        for(int i = 2;i <= n; i++){
            x = max(stage[i+1],stage[i+1]+dp[i]);
            if(x > ans)ans = x;
            dp[i+1] = x;
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>

#define rep(i,j,k) for(int i=j;i<k;i++)
#define drep(n,m) for(int i=0;i<n;i++)for(int j=0;j<m;j++)
#define fi first
#define se second
#define vi vector<int>
#define INF 1000000000
#define MOD 1000000009
#define pb push_back
#define mp make_pair
typedef long long ll;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

ll gcd(ll a,ll b){
    if(a<b){
        int t=a;a=b;b=t;
    }
    int c=a%b;
    while(c!=0){
        a=b;
        b=c;
        c=a%b;
    }
    return b;
}

ll lcm(ll a,ll b){
    return a*b/gcd(a,b);
}

bool isPrime(int x){
    if(x==1)return false;
    if(x==2)return true;
    if(x%2==0)return false;
    for(int i=3;i<=sqrt(x);i+=2){
        if(x%i==0)return false;
    }
    return true;
}

int main() {
    int n;
    while(cin>>n){
        if(n==0)break;
        vector<ll> v(n),rui(n);
        rep(i,0,n){
            cin>>v[i];
            if(i==0)rui[0]=v[0];
            else rui[i]=rui[i-1]+v[i];
        }
    
    ll ans=-100000000000000000;
    rep(i,0,n-1){
        rep(j,i+1,n){
           ans=max(ans,rui[j]-rui[i]);
        }
    }
    rep(i,0,n){
        ans=max(ans,rui[i]);
    }
    cout<<ans<<endl;
    }
    
  
    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

char s[10000];

int main()
{
	while(1)
	{
		int n;
		scanf("%d",&n);
		if(n == 0)
		{
			break;
		}
		int maxw = 0;
		int ms = 0;
		int now = 0;
		for(int i = 0; i < n; i++)
		{
			int w;
			scanf("%d",&w);
			now += w;
			maxw = max(maxw,now - ms);
			ms = min(ms,now);
		}
		printf("%d\n",maxw);
	}
	return 0;
}

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

int a[5001] = {0};
int main() {
  int n;
  while(cin >> n) {
  if(n == 0) break;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int maxsum = 0;
  int sum = 0;
  for(int i = 1; i <= n-1; i++){
    for(int j = i + 1; j <= n;j++) {
      for(int k = i; k <= j-1;k++) {
	sum += a[k];
      }
      if(sum > maxsum)
	maxsum = sum;
      sum = 0;
    }
  }
  cout << maxsum << endl;
  }
}

C++: #include <stdio.h>

int ans ;
int sum ;
int n ;
int p ;
int q ;
int num[5010] ;

int main()
{
	while ( scanf ( "%d" , &n ) == 1 && n ) {
		for ( int i = 0 ; i < n ; i++ ) {
			scanf ( "%d" , &num[i] ) ;
		}
		ans = -2147483647 ;
		sum = 0 ;
		for ( int i = 0 ; i < n ; i++ ) {
			sum += num[i] ;
			if ( sum < 0 ) {
				sum = 0 ;
				p = i + 1 ;	
			}
			if ( sum > ans ) {
				ans = sum ;	
				q = i ;
			}
		}
		printf ( "%d\n" , ans ) ;
	}
	return 0 ;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int dy[] = { 0, 0, 1, -1, 0 };
int dx[] = { 1, -1, 0, 0, 0 };

int a[5000];
int dp[5000];

int main(){
	ios::sync_with_stdio(false);

	int n;
	int smax;

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

		REP(i, n)	scanf("%d", &a[i]);

		dp[0]=a[0];
		smax =a[0];

		FOR(i, 1, n){
			dp[i]=max(dp[i-1]+a[i], a[i]);
		}

		REP(i, n){
				smax=max(dp[i], smax);
		}

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

C++: #include<stdio.h>

int main(){

	int i,j,n;
	long item[5000],sum,Max;

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

		for(i=0;i<n;i++){
			scanf("%ld",&item[i]);
			for(j=i;i<n;j++){
				sum+=item[j];
				if(Max<sum) Max=sum;
				
			}
					
		}
		printf("%ld\n",Max);


	}



	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main()
{
    int n,t,mx;
    while(cin >> n, n)
    {
        mx = -(1<<30);
        int dt[5000]={0};
        for(int i=0; i<n; i++)
        {
            cin >> t;
            for(int j=0; j<=i; j++)
            {
                dt[j]+=t;
                mx=max(mx,dt[j]);
            }
        }
        cout << mx << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
using namespace std;
long int maxsum(long int x,vector<long int> a){
	
	vector<long int> s(x+1,0);
	long int max;
	for(int i=1;i<=x;i++){
		if(s[i-1]<0)
			s[i]=a[i];
		else
			s[i]=s[i-1]+a[i];
	}
	max = *max_element(s.begin(),s.end());
	return max;
	
}

int main(){
	
	long int n;
	while(cin>>n,n!=0){
		
		vector<long int> a(n+1,0);
		
		for(long int i=1;i<=n;i++)
			cin>>a[i];
		
		cout<<maxsum(n,a)<<endl;
	}
	
 	
}