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 <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;

int main(){
	int N;
	int x;
	while(cin>>N&&N){
		int index=0;
		int ans = 0;
		int sum[5000+1];
		FOR(i,1,N+1){
			cin>>sum[i];
			sum[i] += sum[i-1];
		}
		FOR(i,1,N+1){
			FOR(j,i,N+1){
				ans = max(ans,sum[j]-sum[i-2]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 

int main(void){  
  int n;
  while(cin>>n,n){
    int sum = 0, maxs = 0, a[10000];
    REP(i,n)
      cin >> a[i];
    FOR(i,0,n){
      sum += a[i];
      maxs = max(sum, maxs);
    }
    FOR(i,0,n){
      sum -= a[i];
      maxs = max(sum, maxs);
    }
    cout << maxs << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit 
//	freopen("testcase.mss", "r", stdin);
	string str = "";
	int n;

	while (1){
		getline (cin, str );
		stringstream ssn(str);
		ssn >> n;
		if (n == 0){
			break;
		} // end if
		vector <int> a(n, 0);
		int i,j;

		vector <int> sum (n, 0);

		for (i = 0; i < n; ++i){
			getline (cin, str );
			stringstream ss(str);
			ss >> a[i];
		} // end for
		for (i = 0; i < n; ++i){
			sum[i] = accumulate (a.begin(), a.begin()+i+1, 0 );
//			cout << "sum[" << i << "]:" << sum[i] << endl;
		} // end for

		int maxn = INT_MIN;
		for (i = 0; i < n; ++i){
			for (j = i + 1; j < n; ++j){
				 maxn = max (maxn , sum[j] - sum[i-1]);
			} // end for
		} // end for
		cout << maxn << endl;			

	} // end while

	return 0;
}

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

typedef int64_t Int;
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

bool print_space_enable = false;
void print() { 
    cout << endl; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) cout << " ";
    cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << std::endl;
}

int main() {
    Int n;
    while (cin >> n and n) {
        vector<Int> a(n);
        for (Int i = 0; i < (Int)n; i++) {
            cin >> a[i];
        }
        vector<Int> b(n + 1, -INF);
        b[0] = 0;
        for (Int i = 0; i < (Int)n; i++) {
            b[i + 1] = max(b[i] + a[i], a[i]);
        }
        Int res = -INF;
        for (Int i = 0; i < (Int)n; i++) {
            res = max(res, b[i + 1]);
        }
        print(res);
    }
    return 0;
}

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

using namespace std;

int main() {
    vector<int> cum;
    int n, sum, tmp;
    
    while(true) {
        cin >> n;
        if(n == 0) { break; }
        
        cum.resize(n+1);
        cum[0] = 0;
        sum = (-2147483647);
        
        for(int i = 1; i <= n; i++) {
            cin >> tmp;
            cum[i] = cum[i-1] + tmp;
        }

        for(int i = 0; i < n; i++) {
            for(int j = i+1; j <= n; j++) {
                sum = max(sum, cum[j] - cum[i]);
            }
        }
        cout << sum << endl;
    }
    return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000
#define MOD 1000000007
#define EPS 1E-12

const int MAX_N = 5000;
int n;
int a[MAX_N+2];

int main()
{
  while((cin >> n) && n){
    rep(i, n) cin >> a[i+1];
    rep(i, n) a[i+2] += a[i+1];

    int res = 0;
    rep(i, n+1) rep(j, i) maxup(res, a[i] - a[j]);

    cout << res << endl;
  }

  return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <map>
#define INF 100000000
#define rep(i,a) for(int i=0;i<(a);i++)

using namespace std;
typedef long long ll;


int main(){
    int n;
    while(1){
        cin>>n;
        if(n==0)break;
        int dp[5000][5000]={};
        int a[5000]={};

        rep(i,5000)rep(j,5000)dp[i][j]=-INF;
        
        rep(i,n){
            cin>>a[i];
        }
        rep(i,n) dp[i][i]=a[i];
        
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                dp[i][j]=dp[i][j-1]+a[j];
            }
        }
        int maxi=-INF;
        rep(i,n){
            rep(j,n){
                maxi=max(maxi,dp[i][j]);
            }
        }
        cout<<maxi<<endl;
        
    }
    return 0;
}