Go: package main

import "fmt"

func main() {
	var result []int
	for {
		var n, a, maxSum int
		fmt.Scan(&n)

		if n == 0 {
			break
		}

		maxSum = 0
		sum := 0
		for i := 0; i < n; i++ {
			fmt.Scan(&a)
			if i == 0 {
				sum = a
				maxSum = sum
			} else if sum+a < a {
				sum = a
				if maxSum < sum {
					maxSum = sum
				}
			} else {
				sum = sum + a
				if maxSum < sum {
					maxSum = sum
				}
			}
		}

		result = append(result, maxSum)
	}

	for _, v := range result {
		fmt.Println(v)
	}
}

C++: #include<algorithm>
#include<functional>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

typedef unsigned long long ull;
typedef long long ll;

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

using namespace std;

typedef pair<int ,int > P;

int n;
vector<int> vec;

int main()
{
	while(cin>>n)
	{
		if(!n) return 0;
		int ans=-INF;
		rep(i,n)
		{
			int a;
			cin>>a;
			vec.pb(a);
			ans=max(ans,a);
		}
		rep(i,n)
		{
			int sum=0;
			for(int j=i;j<n;j++)
			{
				sum+=vec[j];
				ans=max(ans,sum);
			}
		}
		cout<<ans<<endl;
		vec.clear();
	}
	return 0;
}

C++: #include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000001
#define IN_MAX -223372036854775808

using namespace std;
#define ll long long
#define dbg if(0)



int main(){
    while(true){
        int n;
        ll ans= IN_MAX;
        cin>>n;
        if (n==0)break;

        ll rui[n+1];
        rui[0]=0;
        for(int i=1;i<=n;i++){
            cin >>rui[i];
            rui[i]+=rui[i-1];
            dbg cout <<rui[i]<<" ";
        }
        //次を見る
        //今までの最小値を更新したか？
        //したなら暫定解を確定しansと比較。最大値=最小値=発見した最小値にする
        //してなければ最大値の更新をする
        //見る場所が一番後ろまで行ったら終了。
        ll nmin=rui[0],nmax=rui[0];
        for(int i=0;i<=n;i++){
            if (rui[i]<nmin){
                ans=max(nmax-nmin,ans);
                nmax=rui[i];
                nmin=rui[i];
            }else{
                if(rui[i]>nmax){
                    nmax=rui[i];
                }
            }
            dbg cout <<nmax<<" "<<nmin<<endl;
        }
        ans=max(nmax-nmin,ans);
        dbg cout << endl;
        cout<<ans<<endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <climits>
using namespace std;
#define rep2(i,from,to) for(int i=(from); i<(to); ++(i))
#define rep(i,to) rep2(i,0,to)
int main() {
	int n;
	while(cin >> n && n) {
		int a[5001];
		int Max = INT_MIN;
		rep(i,n) {
			cin >> a[i]; 
		}
		rep(i,n) {
			int sum = 0;
			rep2(j,i,n) {
				sum += a[j];
				Max = max(Max, sum);
			}
		}
		cout << Max << endl;
	}
	return 0;
}

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

C++: #include <iostream>
#define MAX(a,b) (((a)>(b))?(a):(b))

using namespace std;
typedef long long int lli;
int main(void){
	int n;
	lli a[5000];
	while(cin>>n,n){
		for(int i=0;i<n;i++)cin>>a[i];
		lli max=-2000000;
		lli sum=0;
		lli msum=0;
		bool isminus=false;
		int t=0;
		max=a[0];
		if(a[0]>=0) sum+=a[0];
		for(int t=1;t<n;t++){
			// cout<<"debug:"<<max<<" "<<sum<<" "<<msum<<endl;
			if(a[t]>=0){
				if(!isminus){ // ??£????????£
					sum+=a[t];
					max=MAX(sum,max);
				}
				else { // ???????????£
					isminus=false;
					if(sum+msum>=0){ // ????????????????¨?????????????????????´???
						max=MAX(sum,max);
						sum+=msum;
						sum+=a[t];
						msum=0;
					}else{ // ???????????£??§??????????????????????????´???
						max=MAX(sum,max);
						sum=0;
						sum+=a[t];
						msum=0;
					}
				}
			}else{
				if(isminus)msum+=a[t];
				else{
					isminus=true;
					msum+=a[t];
				}
			}
		}
		cout<<MAX(sum,max)<<endl;
	}
	return 0;
}

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

int a[5001];

int sumation(int i, int n){
	int res=0;
	for(int k=0; k<n; k++){
		res += a[i+k];
	}
	return res;
}

int main(void){

	for(int i=0; i<5001; i++) a[i] = 0;

	int n;
	while(cin >> n){
		
		if(n==0) break;
		for(int i=0; i<n; i++){
			int tmp;
			cin >> tmp;
			a[i] = tmp;
		}

		int res = 0;
		for(int i=1; i<=n; i++){	
			int rres = 0;
			for(int j=0; j<(n-i+1); j++){	
				if( rres < sumation(j,i) ) rres = sumation(j,i);
			}
			if( res < rres ) res = rres;
		}
		cout << res << endl;

		for(int i=0; i<5001; i++) a[i] = 0;
	}

	return 0;
}

C++: #include <iostream>


using namespace std;




int main(){
	

	int n;
	int a[5000];
	int max;
	int temp;
	
	while(cin>>n&&n){
	for(int i=0;i<n;i++)cin>>a[i];
	max=a[0];
	
	for(int i=0;i<n;i++){
		temp=0;
		for (int j=i; j<n; j++) {
			temp+=a[j];
		
		if (max<temp) {
			max=temp;
		}
		}
	}
	
	
	cout << max <<endl;
	}
		
	return 0;
}

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

int main() {
	int n;
	long max, sum, ans;
	while (true) {
		cin >> n;
		if (n == 0) break;
		vector<int> array(n);
		for (int i = 0;i < n;i++) {
			cin >> array[i];
		}

		for (int j = 0;j < n - 1;j++) {
			max = array[j] + array[j + 1];
			sum = array[j];
			for (int i = j + 1;i < n;i++) {
				sum += array[i];
				if (sum > max) {
					max = sum;
				}
			}
			if (j == 0) {
				ans = max;
			}
			else if (max > ans) {
				ans = max;
			}
		}
		if (array[n - 1] > ans) {
			ans = array[n - 1];
		}


		cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MIN (-(1<<20))
vector<int> dp(5001);
vector<int> v(5001);
int main()
{
	int n;
	while (true) {
		cin >> n;
		if (n == 0) break;
		fill(dp.begin(), dp.end(), MIN);
		for (int i = 0; i < n; ++i) cin >> v[i];
		dp[0] = v[0];
		for (int i = 1; i < n; ++i)
			dp[i] = max(v[i], dp[i-1] + v[i]);
		sort(dp.begin(), dp.begin() + n);
		cout << dp[n-1] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
int main(){
	int n;
	while(cin >> n){
		if(n==0) break;
		vector<int> a;
		vector<int> s;
		for(int i=0;i<n;++i){
			int t;
			cin >> t;
			a.push_back(t);
		}
		for(int i=0;i<a.size();++i){
			for(int j=i+1;j<=a.size();++j){
				s.push_back(accumulate(a.begin()+i,a.begin()+j,0));
			}
		}
		sort(s.begin(),s.end(),greater<int>());
		cout << s[0] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
int main(){
	int n;
	while(cin >> n){
		if(n==0) break;
		vector<int> a;
		vector<int> s;
		int sum=0;
		for(int i=0;i<n;++i){
			int t;
			cin >> t;
			a.push_back(t);
		}
		for(int i=0;i<a.size();++i){
			for(int j=i+1;j<=a.size();++j){
				s.push_back(accumulate(a.begin()+i,a.begin()+j,0));
			}
		}
		stable_sort(s.begin(),s.end(),greater<int>());
		cout << s[0] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

int main(){
	int n, a;
	while(cin >> n, n){
		n++;
		vector<int> t(n);
		t[0] = 0;
		/*for(int i = 1;i < n;i++){
			cin >> v[i];
			t[i] = max(t[i-1]+v[i], v[i]);
		}*/
		for(int i = 1;i < n;i++){
			cin >> a;
			t[i] = max(t[i-1]+a, a);
		}
		cout << *max_element(t.begin(), t.end()) << endl;
	}
	return 0; 
}

/*
7
-5
-1
6
4
9
-6
-7
*/