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 <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MAXN 5010
int main(void) {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        int a[MAXN], sum[MAXN];
        for (int i = 0; i < n; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
        sum[0] = 0;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            sum[i] = sum[i-1] + a[i];
        }
        int ret = a[1];
        for (int i = 0; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                ret = max(sum[j]-sum[i], ret);
            }
        }
        cout << ret << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
 
typedef long long ll;
typedef pair<int, int> ii;

#define EPS 1e-10

int main(){

	int n;
	while(cin >> n, n){
		vector<int> a(n);
		rep(i, n) cin >> a[i];
		
		ll sum = 0;
		ll ans = -LLINF;
		rep(i, n){
			sum += a[i];
			ans = max(ans, sum);
			if(sum < 0) sum = 0;
		}
		
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

#define N 2<<30;

int main(){

  int n;


  while(cin >> n){
    if(n == 0) break;
    vector<int> V;
    int status;
    int in;
    int sum = 0;
    for(int i = 0; i < n; i++){
      cin >> in;
      if(i == 0){
	if(in < 0){
	  status = -1;
	  V.push_back(in);
	}
	else if(in >= 0)status = 1;

      } else {

	if(in < 0 && status > 0){
	  status = -1;
	  V.push_back(sum);
	  sum = 0;
	  V.push_back(in);
	  continue;
	}else if(in < 0 && status < 0){
	  V.push_back(in);
	  continue;
	}else if(in >= 0 && status < 0){
	  status = 1;
	  V.push_back(sum);
	  sum = 0;
	}
      }
      sum += in;
    }
    if(sum >= 0) V.push_back(sum);

    int max = -1*N;
    
    for(int i = 0; i < V.size(); i++){
      sum = 0;
      for(int j = i; j < V.size(); j++){
	sum += V[j];
	if(max < sum) max = sum;
      }
    }
    
    cout << max << endl;
  }
  return 0;
}

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

int maximum_sub_sequense(vector<int> &s)
{
    int max = 0;

    for (auto i = s.begin(); i != s.end(); i++) {
        int sum = 0;
        for (auto j = i; j != s.end(); j++) {
            sum += *j;
            if (sum > max)
                max = sum;
        }
    }

    return max;
}

int main()
{
    for (int n; cin >> n && n;) {
        vector<int> sequence(n);
        for (int x; n-- && cin >> x;)
            sequence[n] = x;
        cout << maximum_sub_sequense(sequence) << endl;
    }

    return 0;
}

C++: /*
 * AOJ-Volume0-0022
 */
#include <iostream>
#include <limits.h>
#include <vector>
#include <cstdio>

using namespace std;

typedef long long ll;

/*
 * Find maximum sum.
 *   array: number list
 */
ll findMaximumSum(vector<int> &array){
  ll sumValue = array[0];
  ll maxValue = INT_MIN;
  int length = array.size();

  for(int i = 1; i < length; i++){
    ll value = array[i];

    if(sumValue < 0){
      sumValue = value;
    }else{
      sumValue += value;
    }

    if(maxValue < sumValue){
      maxValue = sumValue;
    }
  }

  return maxValue;
}

void solve(int n){
  vector<int> a(n);

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

  cout << findMaximumSum(a) << endl;
}

int main(){
  int n;

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

    solve(n);
  }

  return 0;
}

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

const int N = 5e3 + 11;
int a[N];

int main() {
	ios :: sync_with_stdio(false);
	int n;
	while(cin >> n) {
		if(n == 0) break;
		for(int i = 0; i < n; i ++)
			cin >> a[i];
		int ans = a[0], pre = min(a[0], 0);
		for(int i = 1; i < n; i ++) {
			a[i] += a[i - 1];
			ans = max(ans, a[i] - pre);
			pre = min(pre, a[i]);
		}
		cout << ans << '\n';
	}
	return 0;
}

C++: #include <stdio.h>

int seqsum(int n){
	int a = 0;
	int b[5000];
	int c = 0;
	int s = 0;
	int f = 0;
	scanf("%d", &a);
	b[0] = a;
	f = 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];
	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 > f ? m : f;
}

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

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int 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)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
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 !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  while (cin >> n, n) {
    vi a(n);
    rep(i, n) cin >> a[i];
    int m = -INF;
    rep(i, n) {
      int c = 0;
      loop(j, i, n) {
        c += a[j];
        cmax(m, c);
      }
    }
    cout << m << endl;
  }
}

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

int maximum_sum_seq(vector<int> seq,int n) {
  int max_sum = seq[0];
  int cur = seq[0];
  for(int i=0;i<n;i++) {
    cur = seq[i];
    for(int j=i+1;j<n;j++) {
      cur += seq[j];
      max_sum = max(max_sum,cur);
    }
  }
  return max_sum;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  while(cin >> n) {
    if(n == 0) break;
    vector<int> seq(n);
    for(int i=0;i<n;i++) {
      cin >> seq[i];
    }
    cout << maximum_sum_seq(seq,n) << endl;
  }
}