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 <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

int main() {
  // ifstream cin("../test.txt");
  int n;
  while (true) {
    cin >> n;
    if (!n) break;
    vector<int> a(n);
    vector<int> sub_sum(n+1); // a[0] + ... + a[i]
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sub_sum[0] = 0;
    int max_sum = -1000000000;
    for (int i = 0; i <= n; i++) {
      sub_sum[i] = sub_sum[i-1] + a[i-1];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        max_sum = max(max_sum, sub_sum[i] - sub_sum[j]);
      }
    }
    cout << max_sum << endl;
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <complex>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
const double PI = 3.141592653589793238462643383279502884L;
const int LARGE_PRIME = 1000000007;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++)
#define rtr(c, i) for(auto i = (c).rbegin(); i != (c).rend(); i++)
#define repfn(i,a,cont,next) \
  for(auto i = (a); [=](){return(cont);}(); i = [=](){return(next);}())
#define repby(i,a,b,s) repfn(i,a,i<b,i+s)
#define repab(i,a,b) repby(i,a,b,1)
#define rep(i,b) repab(i,0,b)
#define pb push_back
#define sz(c) int((c).size())

int main(int argc, char **argv)
{
  for (;;) {
    int n;
    vi v;

    cin >> n;
    if (!n)
      break;

    while (n--) {
      int tmp;
      cin >> tmp;
      v.pb(tmp);
    }

    vi w(v);
    for (int i = 1; i < sz(v); i++) {
      v[i] = max(v[i-1] + v[i], v[i]);
    }
    cout << *max_element(all(v)) << endl;
  }
  return 0;
}

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

int main(){
  int n, num, a, maxx;
  int ans[N], ac;
  ac = 0;

  while(cin >> n){
    if(n==0){
      ans[ac] = -100001;
      break;
    }

    num = maxx = 0;

    for(int i=0;i<n;i++){
      cin >> a;
      num += a;
      if(num<0) num = 0;
      maxx = max(maxx,num);
    }
    ans[ac] = maxx;
    ac++;
  }

  for(int i=0;ans[i]!=-100001;i++)
    cout << ans[i] << endl;

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <climits>
#include <cmath>
#include <cfloat>

using namespace std;

long int a[5000],ma,n;

int main()
{
	
	while(cin >> n, n)
	{
		ma = INT_MIN;
		
		for( int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		
		for( int j = 0; j < n; j++)
		{
			int sum = 0;
			
			for( int i = j; i < n; i++)
			{
				sum += a[i];
				
				if( sum > ma )
					ma = sum;
				
			}
		}
		
		cout << ma << endl;
	}
	
	return 0;
}

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

int main() {
    int n, a, sum[5010];
    while (cin >> n, n) {
        memset(sum, 0, sizeof sum);
        cin >> a;
        sum[0] = a;
        for (int i=1; i<n; ++i) {
            cin >> a;
            sum[i] = sum[i-1] + a;
        }
        int mx = -100000;
        for (int i=0; i<n; ++i) {
            for (int j=i; j<n; ++j) {
                if (i-1 < 0) {
                    mx = max(mx, sum[j]);
                } else {
                    mx = max(mx, sum[j] - sum[i-1]);
                }
            }
        }
        cout << mx << endl;
    }
    return 0;
}

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

using namespace std;

template <typename Type>
long long Maximum_Sum_Sequence(Type N[], int size)
{
	long long max = LLONG_MIN;

	for (int i = 0; i < size; i++)
	{
		long long sum = 0;

		for (int j = i; j < size; j++)
		{
			sum += N[j];

			if (sum > max) { max = sum; }
		}
	}

	return max;
}

int main()
{
	const int DATASETS = 1000;

	int n[DATASETS], a[DATASETS][5000], c = 0;

	while (true)
	{
		cin >> n[c];

		if (n[c] == 0) { break; }

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

		c++;
	}

	for (int i = 0; i < c; i++)
	{
		cout << Maximum_Sum_Sequence(a[i], n[i]) << endl;
	}

	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define np string::npos
#define X real()
#define Y imag()
#define value(x,y,w,h) (x >= 0 && x < w && y >= 0 && y < h)
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

typedef long long ll ;
typedef deque<int> di ;
typedef deque<ll> dl ;
typedef map<string,int> dict;
typedef complex<double> comd;
typedef pair<int,int> pii;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

double CalcDist(comd p1, comd p2){
  return sqrt(pow(p1.X - p2.X,2.0) + pow(p1.Y -p2.Y,2.0));
}

  template <typename T>
void out(deque < T > d)
{
  for(size_t i = 0; i < d.size(); i++)
  {
    debug(d[i]);
  }
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while(cin >> n){
    if(n == 0) break;
    vector<int> a(n+1,0),b(n+1,0);
    int ans =0,sum=0;
    rep(i,n){
      cin >> a[i];
      sum += a[i];
      b[i+1] = sum;
    }
    rep(i,n+1){
      REP(j,i,n+1){
        ans = max(ans,b[j] - b[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  while( cin >> n ,n)
    {
      int a[5101] = {0};
      for(int i = 0 ; i < n ; i++ )
	{
	  cin >> a[i];
	}

      int dp[5101] = {0},mx = 0;

      dp[0] = a[0];
      for(int i = 1 ; i < n ; i++ ){
	dp[i] = max(dp[i-1] + a[i],a[i]); 
	mx = max(dp[i],mx);
      }
      cout << mx << endl;
    }
  return 0;
}

C++: #include <iostream>
using namespace std;
#define N_MAX 5000
int array[N_MAX];
int main(){
	int n;
	int num;
	while(cin>>n&&n){
		int ans=0;
		for(int i=0;i<n;i++){
			cin>>num;
			if(i==0)array[i]=num;
			else{
				array[i]=max(array[i-1]+num,num);
			}
			ans=max(ans,array[i]);
		}
		cout<<ans<<endl;
	}
	return 0;
}