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 <bits/stdc++.h>
using namespace std;
#define INF 9999999999

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

        int ret = -INF;
        for(int i=0; i<n-1; i++) {
            for(int j=i+1; j<n; j++) {
                ret = max(a[j] - a[i], ret);
            }
        }

        cout << ret << endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main(){
	int n;

	while(cin >> n){
		int max = 0;
		int soremade = 0;
		int mae = 0;
		int ima = 0;

		if(n==0) break;
		
		cin >> mae;

		for(int i=1;i<n;i++){
			if(mae > soremade+mae){
				soremade = mae;
			}else{
				soremade += mae;
			}
			
			cin >> ima;
			
			if(max < ima+soremade){
				max = ima+soremade;
			}
			mae = ima;
		}

		cout << max << endl;
	}

	return 0;
}

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

int main() {

  long long int n = 0;

  while( cin >> n, n) {

    long long int max_ = -10000000;

    int array[n];
    for(int i = 0; i < n; i++){
      cin >> array[i];
    }

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

  return 0;

}

C++: #include <iostream>
#include <climits>
int main()
{
  while (1) {
    int n;
    std::cin >> n;
    if (n == 0) break;
    int max = -INT_MAX;
    int a[n];
    for (int i = 0;i < n; i++) {
      std::cin >> a[i];
    }
    for (int i  = 0; i < n; i++) {
      if (a[i] < 0) continue;
      int temp = a[i];
      for (int j = i + 1; j < n; j++) {
        temp += a[j];
        if (temp > max) max = temp;
      }
    }
    std::cout << max << std::endl;
  }
}

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

int a[5005];
int dp[5005];
int n;

int main(){



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

        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        long long p = 0;
        for(int i=0;i<n;i++){
            long long k = a[i];
            for(int j=i+1;j<n;j++){
                k = k + a[j];
                p =max(p,k);
            }
        }

        cout<<p<<"\n";

    }

}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define EXIST(v,i) (find(all(v), i) != (v).end())
#define ERASE(v,i) (v).erase(remove(all(v), i), (v).end())
#define INDEX(v,i) (find(all(v), i) - (v).begin())
#define rep(i,n) for(int i = 0; i < (int)n; ++i)
#define debug(x) cerr << #x << " = " << (x) << endl;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

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

int main() {
	for(int n; cin >> n && n;) {
		vector<int> arr(n);
		vector<int> cur(n);
		rep(i,n) cin >> arr[i];
		if(arr[0] > 0) cur[0] = arr[0];
		for(int i = 1; i < n; i++)
			cur[i] = max(arr[i], cur[i - 1] + arr[i]);
		int vmax = *max_element(all(cur));
		cout << (vmax == 0 ? *max_element(all(arr)) : vmax) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        int plus[5000] = {0}, max = -1000000, input;
        rep(i,n){
            cin >> input;
            plus[i]+= input;
            plus[i+1] = plus[i];
            if(max < plus[i]){
                max = plus[i];
            }
        }
        rep(i,n){
            rep(j,i-1){
                if(max < plus[i] - plus[j]){
                    max = plus[i] - plus[j];
                }
            }
        }
        cout << max << endl;
    }
}

C++: /*
	0022:Maximum Sum Sequence
*/
#include	<iostream>
#include	<algorithm>

using namespace std;

int main(void) {
	int n, S[5000] = { 0 }, numsMax = 0;
	while(1) {
		cin >> n;
		if(n == 0) { break; }
		for(int i = 0; i < n; i++) {
			cin >> S[i];
		}
		numsMax = 0;
		for(int i = 0; i < n; i++) {
			int preSum = 0;
			for(int j = i; j < n; j++) {
				preSum += S[j];
				numsMax = max(preSum, numsMax);
			}
		}
		cout << numsMax << endl;
	}
}

C++: #include <cstdio>
#include <algorithm>
#define INF 1e+18
#define MAX_SIZE 1 << 17
using namespace std;

typedef long long ll;

int n,k;
ll v[100000];
ll segMax[2][2 * MAX_SIZE - 1],segAdd[2][2 * MAX_SIZE - 1];

void add(int num,int a,int b,ll x,int k = 0,int l = 0,int r = n){
	if(b <= l || r <= a) return;
	if(a <= l && r <= b){
		segAdd[num][k] += x;
		return;
	}
	add(num,a,b,x,k * 2 + 1,l,(l + r) / 2);
	add(num,a,b,x,k * 2 + 2,(l + r) / 2,r);
	segMax[num][k] = max(segMax[num][k * 2 + 1] + segAdd[num][k * 2 + 1],segMax[num][k * 2 + 2] + segAdd[num][k * 2 + 2]);
}

ll get(int num,int a,int b,int k = 0,int l = 0,int r = n){
	if(b <= l || r <= a) return -INF;
	if(a <= l && r <= b) return segAdd[num][k] + segMax[num][k];
	return max(get(num,a,b,k * 2 + 1,l,(l + r) / 2),get(num,a,b,k * 2 + 2,(l + r) / 2,r)) + segAdd[num][k];
}

void init(){
	int t = 1;
	while(t < n + 1) t *= 2;
	for(int i = 0;i < t * 2 - 1;i++){
		segMax[0][i] = -INF;
		segMax[1][i] = -INF;
	}
	segMax[0][t - 1] = 0;
	for(int i = 0;i < n;i++) segMax[0][i + t] = segMax[0][i + t - 1] + v[i];
	ll ma = -INF;
	for(int i = n;i >= 0;i--) {
		ma = max(ma,segMax[0][i + t - 1]);
		segMax[1][i + t - 1] = ma - segMax[0][i + t - 1];
	}
	for(int i = t - 2;i >= 0;i--) {
		segMax[0][i] = max(segMax[0][i * 2 + 1],segMax[0][i * 2 + 2]);
		segMax[1][i] = max(segMax[1][i * 2 + 1],segMax[1][i * 2 + 2]);
	}
	n = t;
}

int main(){
	scanf("%d %d",&n,&k);
	for(int i = 0;i < n;i++) scanf("%lld",&v[i]);
	init();
	for(int i = 0;i < k - 1;i++){
		
	}
	printf("%lld\n",get(1,0,n));
	return 0;
}