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 <algorithm>
#include <cstdio>
#include <deque>
#include <numeric>
#include <vector>

using namespace std;

template <typename C>
vector<int> compact(const C& container) {
  vector<int> compacten;
  int sign = 1;
  int acc = 0;
  for (auto n : container) {
    if (n == 0) { continue; }
    if (sign * n > 0) {
      acc += n;
    } else {
      compacten.push_back(acc);
      acc = n;
      sign *= -1;
    }
  }
  compacten.push_back(acc);
  return compacten;
}

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0) { break; }

    deque<int> seq;
    for (int i = 0; i < n; ++i) {
      int x;
      scanf("%d", &x);
      seq.push_back(x);
    }
    while (seq.size() != 0 && seq.front() <= 0) { seq.pop_front(); }
    while (seq.size() != 0 && seq.back() <= 0) { seq.pop_back(); }

    auto compacten_seq = compact(seq);
    vector<int> partial_sums = { compacten_seq.back() };
    for (int i = compacten_seq.size() - 1; i > 0; i -= 2) {
      int s = partial_sums.back() + compacten_seq[i - 1];
      partial_sums.push_back(compacten_seq[i - 2] + (s < 0 ? 0 : s));
    }
    int max_sum = accumulate(
        partial_sums.begin(),
        partial_sums.end(),
        partial_sums.front(),
        [](int a, int b) { return max(a, b); });
    printf("%d\n", max_sum);
  }

  return 0;
}

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

using namespace std;

void solve()
{
	int N;
	while(cin >> N, N)
	{
		vector<int> Vec(N);
		for(int i = 0; i < N; ++i)
		{
			cin >> Vec[i];
		}
		int Max = 0;
		for(int i = 0; i < N; ++i)
		{
			int sum = 0;
			for(int j = i; j < N; ++j)
			{
				sum += Vec[j];
				if(sum > Max)
				{
					Max = sum;
				}
			}
		}
		cout << Max << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int main() {
	int N;
	while (cin >> N) {
		if (N == 0) return 0;
		int A[5000] = {}, DP[5010][2] = {}, MAX = -1000000;
		for (int i = 0; i < N; i++) cin >> A[i], MAX = max(MAX, A[i]);
		for (int i = 0; i < N; i++) {
			DP[i + 1][1] = max(DP[i][1] + A[i], DP[i + 1][1]);
			DP[i + 1][1] = max(A[i], DP[i + 1][1]);
			DP[i + 1][0] = max(DP[i][0], DP[i + 1][1]);
		}
		if (DP[N][0] != 0) cout << DP[N][0] << endl;
		else cout << MAX << endl;
	}
}

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 = "";
	string res = "";
	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,k;

		for (i = 0; i < n; ++i){
			getline (cin, str );
			stringstream ss(str);
			ss >> a[i];
		} // end for
		vector <int> sum;
		for (i = 0; i < n; ++i){
			for (j = i + 1; j < n; ++j){
				int s = 0;
				for (k = i; k <= j; ++k){
					s += a[k];
				} // end for
				sum.push_back(s);
			} // end for
		} // end for
		vector <int>::iterator itM = max_element (sum.begin(), sum.end() );
		cout << *(itM) << endl;

	} // end while

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
        int a[5001] = { 0 };
        int n;
    while (1) {
        cin >> n;
        if (n == 0)break;
       // a[0] = 0;
        int maxv = -100000;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++) {
            a[i] = max(a[i], a[i] + a[i - 1]);
            maxv = max(maxv, a[i]);
        }
        cout << maxv << endl;
    }
}

C++: #include<iostream>
using namespace std;
int main(){
	long int n,a[5000],saidai=-100000;
	while(cin>>n&&n!=0){
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		for(int i=0;n>i;i++){
			for(int x=n;x>=i;x--){
				long int kazu=0;
				for(int y=x;y>=i;y--){
					kazu=kazu+a[y];
				}
				if(kazu>saidai){
					saidai=kazu;
				}
			}
		}
		cout<<saidai<<endl;
	}
}

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 ( sum + ar[ now_check ] <= 0 ) {
		solve( now_check + 1, 0 );
	}
	else {
		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;
}