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++: // clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

template<typename T> vector<T> maximumSubArray(const vector<T> &v) {
  int n = v.size();
  vector<T> dp(n);
  dp[n - 1] = v[n - 1];
  for (int i = n - 2; i >= 0; i--) dp[i] = max(v[i], v[i] + dp[i + 1]);
  auto begin = max_element(all(dp));
  int l, r;
  l = r = distance(dp.begin(), begin);
  int maxSum = *begin, acc = 0;
  while (acc != maxSum) acc += v[r++];
  return vector<T>(v.begin() + l, v.begin() + r);
}

main {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) break;
    vector<int> a(n);
    cin >> a;
    auto v = maximumSubArray(a);
    cout << accumulate(all(v), 0LL) << endl;
  }
}

C++: #include <iostream>

using namespace std;

int main(){
  int n;
  while(true){
    cin>>n;
    if(!n)break;
    int seq[n];
    for(int i=0;i<n;i++)
      cin>>seq[i];
    int max = -1e9;
    for(int i=0;i<n;i++){
	int sum = 0;
      for(int j=i;j<n;j++){
	  sum+=seq[j];
	if(sum>max)max = sum;
      }
    }
    cout << max << endl;
  }
  return 0;
}

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

int main() {
  int n, a[5001], sum, mx;
  
  while(cin>>n, n) {
    for(int i = 0; i < n; i++) cin >> a[i];
    mx=0;
    for(int i = 0; i < n; i++) {
      sum=0;
      for(int j = i; j < n; j++) {
	sum+=a[j];
	mx=max(mx, sum);
      }
    }
    cout<<mx<<endl;
  }

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    for(;;){
        long long int n,a[5001]={},ma=0,sum=0;
        cin>>n;
        if(n==0)break;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        ma=0;
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                sum=0;
                for(int k=i;k<=j;k++){
                    sum+=a[k];
                }
                ma=max(ma,sum);
            }
        }
        cout<<ma<<endl;
    }
return 0;
}

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

int main(){
  while(true){
    int n, max=0, tmp=0;
    cin>>n;
    if(n==0)break;
    for(int i=0; i<n; i++){
      int d;
      cin>>d;
      tmp=(tmp>0 ? tmp+d : d);
      if(max<tmp)max=tmp;
    }
    cout<<max;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
typedef long long llong;
typedef vector <llong> vll;

int maxv(vll &x){
	int tmp=x[0];
	for (int i=1; i<x.size(); i++)
		if (x[i]>tmp) tmp=x[i];
	return tmp;
}

int main(void){
	int n;
	vll x;
	while (cin>>n, n!=0){
		int s=LLONG_MIN;
		for (int i=0; i<n; i++){
			int n2;
			cin>>n2;

			for (int j=0; j<x.size(); j++){
				x[j] += n2;
			}
			x.push_back(n2);
			if (i==0 || n2>0) s=max(s, maxv(x));
		}
		cout<<s<<endl;
		x.clear();
	}

	return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

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

#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


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

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

int main(){
	int n, max, sum, a[5001];

	while( cin >> n , n ){
		max = 0;
		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];
				max = (sum>max)? sum : max;
			}
		}
		cout << max << endl;
	}
}

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

int maxsum(const vector<int>& data){
  int maxsumval=0,n=data.size();
  for(int i=0;i<n-1;i++){
    int sum=0;
    for(int j=0;j<n-i;j++){
      sum+=data[i+j];
      if(sum>maxsumval)maxsumval=sum;
    }
  }
  return maxsumval;

}
int main(){
  int n;
  while(cin>>n&&n){
    vector<int> data(n);
    for(int i=0;i<n;i++){
      cin>>data[i];
    }
    int maxsumval=maxsum(data);
    cout<<maxsumval<<endl;
  }

}

C++: #include<stdio.h>

int main(void)
{
	int n,x,t,max,r=1;
	for(;r=1;)
	{
		scanf("%d",&n);
		t=0;
		max=0;
		int a[5000]={0},sum[5000]={0};

		for(x=1;x<=n;x++)
		{
			scanf("%d",&a[x]);
			if(a[x]==0)
			{
				r=0;
				break;
			}
			else if(a[x]>0&&a[x-1]>=0)
			{
				sum[t]+=a[x];
			}
			else if(a[x]<0&&a[x-1]<=0)
			{
				sum[t]+=a[x];
			}
			else if(a[x]>0&&a[x-1]<0)
			{
				t++;
				sum[t]+=a[x];
			}
			else if(a[x]<0&&a[x-1]>0)
			{
				t++;
				sum[t]+=a[x];
			}
		}
		if(r==0)
		{
		}
		else if(sum[0]<0)
		{
			x=1;
		}
		else
		{
			x=0;
		}
		if(r==0)
		{
		}
		else if(sum[t]<0)
		{
			t--;
		}

		int y;
		y=x;
		int b[5000][5000]={0};
		
		if(r==0)
		{
		}
		else if(x==t)
		{
			max=sum[x];
			printf("%d\n",max);
		}
		else
		{
			for(x;x<=t;x++)
			{
				for(y;y<=t;y++)
				{
					if(y==0)
					{
						b[x][y]=sum[y];
					}
					else
					{
						b[x][y]=b[x][y-1]+sum[y];
					}

					if(b[x][y]>max)
					{
						max=b[x][y];
					}
				}
			}
			printf("%d\n",max);
		}
	}
	return 0;
}