Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func max(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func main() {
	sum := make([]int, 5001)
	for {
		n := nextInt()
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			a := nextInt()
			sum[i+1] = sum[i]+a
		}
		ret := sum[1]
		for i := 0; i <= n; i++ {
			for j := i + 1; j <= n; j++ {
				ret = max(ret, sum[j]-sum[i])
			}
		}
		fmt.Println(ret)
	}
}

C++: #include <iostream>
#include <bitset>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
//#include <boost/multiprecision/cpp_int.hpp>

#define BIT(a) (1 << (a))
#define EPS (1e-10)

using namespace std;
//using namespace boost::multiprecision;

const long long MOD = 1000000007;
const int COUNTER_CLOCKWISE = 1;
const int CLOCKWISE = -1;
const int ONLINE_BACK = 2;
const int ONLINE_FRONT = -2;
const int ON_SEGMENT = 0;

class DisjointSet{
    public:
        vector<int> rank, p;

        DisjointSet(){}
        DisjointSet(int size){
            rank.resize(size, 0);
            p.resize(size, 0);
            for (int i = 0; i < size; i++) makeSet(i);
        }

        void makeSet(int x){
            p[x] = x;
            rank[x] = 0;
        }

        bool same(int x, int y){
            return findSet(x) == findSet(y);
        }

        void unite(int x, int y){
            link(findSet(x), findSet(y));
        }

        void link(int x, int y){
            if (rank[x] > rank[y]){
                p[y] = x;
            }else{
                p[x] = y;
                if (rank[x] == rank[y]){
                    rank[y]++;
                }
            }
        }

        int findSet(int x){
            if (x != p[x]){
                p[x] = findSet(p[x]);
            }
            return p[x];
        }
};

class Point{
    public:
    double x, y;

    Point(double x = 0, double y = 0): x(x), y(y) {}

    Point operator+(Point p){return Point(x + p.x, y + p.y);}
    Point operator-(Point p){return Point(x - p.x, y - p.y);}
    Point operator*(double a){return Point(a * x, a * y);}
    Point operator/(double a){return Point(x / a, y / a);}

    bool operator < (const Point &p) const {
        return y != p.y ? y < p.y : x < p.x;
    }

    double norm(){return x * x + y * y;}
};
typedef Point Vector;
typedef vector<Vector> Polygon;

double cross(Vector a, Vector b){
    return a.x*b.y - a.y*b.x;
}

double dot(Vector a, Vector b){
    return a.x * b.x + a.y * b.y;
}

int ccw(Point p0, Point p1, Point p2){
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if (cross(a, b) > EPS)return COUNTER_CLOCKWISE;
    if (cross(a, b) > -EPS)return CLOCKWISE;
    if (dot(a, b) < -EPS)return ONLINE_BACK;
    if (a.norm() < b.norm())return ONLINE_FRONT;

    return ON_SEGMENT;
}

Polygon andrewScan(Polygon s){
    Polygon u, l;
    if (s.size() < 3) return s;
    sort(s.begin(), s.end());
    u.push_back(s[0]);
    u.push_back(s[1]);
    l.push_back(s[s.size()-1]);
    l.push_back(s[s.size()-2]);

    for (size_t i = 2; i < s.size(); i++){
        for (size_t n = u.size(); n >= 2 && ccw(u[n-2], u[n-1], s[i]) == COUNTER_CLOCKWISE; n--){
            u.pop_back();
        }
        u.push_back(s[i]);
    }

    for (int i = (int)s.size() - 3; i >= 0; i--){
        for (size_t n = l.size(); n >= 2 && ccw(l[n-2], l[n-1], s[i]) == COUNTER_CLOCKWISE; n--){
            l.pop_back();
        }
        l.push_back(s[i]);
    }

    reverse(l.begin(), l.end());
    for (size_t i = u.size() - 2; i >= 1; i--) l.push_back(u[i]);

    return l;
}

long long mod_pow(long long x, long long n){
    long long res = 1;
    for(int i = 0;i < 60; i++){
        if(n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

long long mss(long long A[], int l, int r){
    int left;
    int right;
    long long ma;
    long long ma2;
    for (int i = l; i < r; i++){
        if (i == l){
            ma = A[i];
            ma2 = A[i];
            continue;
        }
        ma2 = max(A[i], ma2+A[i]);
        ma = max(ma2, ma);
    }
    return ma;
}

long long A[100005];

int main(){
    cin.tie(0);
    while(1){
        int N;
        cin >> N;
        if (!N) break;
        for (int i = 0; i < N; i++){
            cin >> A[i];
        }
        cout << mss(A, 0, N) << endl;
    }

    return 0;    
}

C++: #include <iostream>
main(){for(int n,i,m,a[5000],j,s;std::cin>>n,n;std::cout<<m<<"\n"){for(i=0;i<n;i++)std::cin>>a[i];for(i=0,m=-1e9;i<n;i++)for(j=i,s=0;j<n;m=s>m?s:m,j++)s+=a[j];}}

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;
		vl sum(N+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 <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) {
		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<iostream>

using namespace std;
int n;
int S[5000];
int a[5000];
int mss(int x){
    int max=a[0];
    int lp=0;
    int rp=x;
    for(int l=lp;l<rp;l++){
        for(int r=l;r<rp;r++){
            int sum=0;
            if (l!=0){
                sum=S[r]-S[l-1];
            }
            else{
                sum =S[r];
            }
            if(sum>max){
                max=sum;
            }
        }
    }
    return (max);
}
int main(){
    while(cin>>n&&n){
        for(int i=0;i<n;i++){
            cin>>a[i];
            
            S[0]=a[0];
            for(int j=1;j<n;j++){
                S[j]=S[j-1]+a[j];
            }
        }
        cout<<mss(n)<<endl;
    }
    return 0;
}

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

using namespace std;

int main() {
    while(true) {
        int N;
        cin >> N;
        if(!N) break;

        queue<int> q;
        int ans = 0;
        int sump = 0, sumn = 0;
        int left = 0;
        while(N--) {
            int n;
            cin >> n;
            q.push(n);
            if(n >= 0) sump += n;
            else sumn += -n;
            while(!q.empty() && sump <= sumn) {
                int np = q.front();
                if(np >= 0) sump -= np;
                else sumn -= -np;
                q.pop();
            }
            ans = max(ans, sump-sumn);
        }
        cout << ans << endl;
    }
    return 0;
}