Go: package main

import (
        "fmt"
)

func main() {
        for {
                var n int
                _, err := fmt.Scanf("%d", &n)
                if err != nil || n == 0 {
                        break
                }

                var sum [5001]int

                for i := 1; i <= n; i++ {
                        fmt.Scanf("%d", &(sum[i]))
                        if i > 1 {
                                sum[i] += sum[i-1]
                        }
                }

                max_sum := sum[1]
                for i := 1; i <= n; i++ {
                        for j := 0; j < i; j++ {
                                if max_sum < sum[i] - sum[j] {
                                        max_sum = sum[i] - sum[j]
                                }
                        }
                }
                fmt.Printf("%d\n", max_sum)
        }
}

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

// warning: include *e
int getBackMaxSum(int* s, int *e) {
	int sum = *e, maxSum = sum;
	for (e--; s <= e; e--) {
		sum += *e;
		maxSum = max(sum, maxSum);
	}
	return maxSum;
}

int main () {
	while (cin >> n, n) {
		int a[n], maxSum = 0x80000000;
		cout << maxSum << endl;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			maxSum = max(maxSum, getBackMaxSum(a, a + i));
		}
		cout << maxSum << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<cmath>
#include<numeric>
using namespace std;


int main(void) {

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

    vector<int> a(n+1, 0);
    int sum = 0;
    for(int i=0; i<n; i++){
      int tmp;
      cin >> tmp;
      sum += tmp;
      a[i+1] += sum;
    }

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

  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>

#define rep(i,j,k) for(int i=j;i<k;i++)
#define drep(n,m) for(int i=0;i<n;i++)for(int j=0;j<m;j++)
#define fi first
#define se second
#define vi vector<int>
#define INF 1000000000
#define MOD 1000000009
#define pb push_back
#define mp make_pair
typedef long long ll;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

ll gcd(ll a,ll b){
    if(a<b){
        int t=a;a=b;b=t;
    }
    int c=a%b;
    while(c!=0){
        a=b;
        b=c;
        c=a%b;
    }
    return b;
}

ll lcm(ll a,ll b){
    return a*b/gcd(a,b);
}

bool isPrime(int x){
    if(x==1)return false;
    if(x==2)return true;
    if(x%2==0)return false;
    for(int i=3;i<=sqrt(x);i+=2){
        if(x%i==0)return false;
    }
    return true;
}

int main() {
    int n;
    while(cin>>n){
        if(n==0)break;
        vector<ll> v(n),rui(n);
        rep(i,0,n){
            cin>>v[i];
            if(i==0)rui[0]=v[0];
            else rui[i]=rui[i-1]+v[i];
        }
    
    ll ans=0;
    rep(i,0,n-1){
        rep(j,i+1,n){
           ans=max(ans,rui[j]-rui[i]);
        }
    }
    rep(i,0,n){
        ans=max(ans,rui[i]);
    }
    cout<<ans<<endl;
    }
    
  
    return 0;
}

C++: #include <iostream>

using namespace std;

const int MIN=-100000;
int main(){
    int n;

    cin>>n;
    while (n){
        int sum=0,max=MIN,t;
        for (int i=0;i<n;++i){
            cin>>t;
            sum+=t;
            if (sum>max) max=sum;
            if (sum<0) sum=0;
        }
        cout<<max<<endl;
        cin>>n;
    }
    return 0;
}

C++: //include----------------------------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>

//using namespace--------------------------------------------------------------------------------------------------------------
using namespace std;

//define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define INF 1 << 25
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << "\n";
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)

//typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ull;

//const------------------------------------------------------------------------------------------------------------------------
const double EPS = 1e-10;
const double PI = acos( -1.0 );

//global-----------------------------------------------------------------------------------------------------------------------
int n;
VI ar;
LL ans = 0;
//function---------------------------------------------------------------------------------------------------------------------
void solve( int now_check, LL sum ) {
	if ( ans < sum ) {
		ans = sum;
	}
	if ( now_check == n ) {
		return;
	}
	if ( ar[ now_check ] < 0 ) {
		solve( now_check + 1, 0 );
	}
	solve( now_check + 1, sum + ar[ now_check ] );
}

int main() {
	while ( scanf( "%d", &n ) ) {
		if ( !n ) {
			return 0;
		}
		ans = 0;
		ar.resize( n );
		rep( i, n ) {
			scanf( "%d", &ar[ i ] );
		}
		solve( 0, 0 );
		printf( "%lld\n", ans );
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

    int n;
    while (cin >> n, n) {
        V a(n);
        rep(i, n) cin >> a[i];

        int ans = -1e9;
        rep(i, n) {
            int t = 0;
            rep2(j, i, n) {
                t += a[j];
                ans = max(ans, t);
            }
        }

        cout << ans << endl;
    }
}