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>
#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 = -999999999;
		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;

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

int main()
{
    int n;
    while (cin >> n, n)
    {
        vector<int> data;
        data.push_back(0);
        rep(i, n)
        {
            int temp;
            cin >> temp;
            data.push_back(data[i] + temp);
        }
        int ans = 0;
        rep(i, n+1)
        {
            REP(j, i, n+1)
            {
                ans = max(ans, data[j] - data[i]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++:  
 #include<iostream>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>
 
 using namespace std;
 
 int arr[5000+5];
 int result[5000+5];
  
 int main()
 {
   int n;
   int count=0;
   while(cin>>n&&(n!=0))
   {
     int sum=0;
     int max=0;
     for(int i=0;i<n;i++)
     {
       cin>>arr[i];
       if(i==0) 
       {
         sum=arr[i];
         max=arr[i];
         continue;         
       }
       if((sum+arr[i])>0)
       { 
         sum=sum+arr[i];
         if(sum>max) max=sum;
         continue;
       }        
       if(arr[i]>max) max=arr[i];
     }       
     result[count++]=max;              
   }
   for(int i=0;i<count;i++) cout<<result[i]<<endl;
   //while(1);      
   return 0;    
 }

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

int main()
{
	int n=1, count = 0;
	int a[10000],ans[10000];
	while (n != 0) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		int sum = 0, max = -10000;
		for (int ai = 0; ai < n - 1; ai++) {
			sum = 0;
			for (int bi = ai; bi < n; bi++) {
				sum = sum + a[bi];
				if (sum > max) {
					max = sum;
				}
			}
		}
		ans[count] = max;
		count++;
	}
	for (int ci = 0; ci < count-1; ci++) {
		cout << ans[ci] << endl;
	}
}

C++: #include<iostream>
using namespace std;
bool same(int a,int b){
  if(a*b>=0) return true;
  return false;
}
int max(int a,int b){
  return(a>b)?(a):(b);
}
int main(){
  int n;
  while(cin>>n){
    if(n==0) break;
    int d;
    int s[n];
    int count = 0;
    int ans=-100001;
    for(int i=0;i<n;i++){
      cin >> d;
      if(ans<d) ans = d;
      if(i==0) s[0] = d;
      else if(same(d,s[count])) s[count] += d;
      else{
	count++;
	s[count] = d;
      }     
    }
    //for(int i=0;i<=count;i++) cout << s[i] << endl;
    if(count==0){
      cout << max(s[0],ans) << endl; 
      continue;
    } 
    for(int i=0;i<=count;i++){
      int h = 0;
      if(ans<h) ans = h;
      for(int j=i;j<=count;j++){
	h += s[j];
	if(ans<h) ans = h;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
int main()
{
	while (!std::cin.eof())
	{
		int n;
		std::cin >> n;
		if (n == 0)
		{
			break;
		}
		std::vector<int>a;
		int max = -100001;
		bool is_all_minus = true;
		for (int i = 0; i < n; i++)
		{
			int foo;
			std::cin >> foo;
			if (foo < 0 && max < foo)
			{
				max = foo;
			}
			is_all_minus &= foo < 0;
			if (i == 0 || a.back() * foo < 0 || a.back() < 0 && foo == 0 || a.back() == 0 && foo < 0)
			{
				a.push_back(foo);
			}
			else
			{
				a.back() += foo;
			}
		}
		if (!is_all_minus)
		{
			if (a.size() == 1)
			{
				max = a[0];
			}
			else
			{
				int begin = a[0] >= 0 ? 0 : 1;
				int temp_max;
				try
				{
					for (int i = begin;;)
					{
						temp_max = a[i];
						if (a.at(i + 1) + a.at(i + 2) > 0)
						{
							temp_max += a[i + 1] + a[i + 2];
							i += 2;
						}
						else
						{
							if (max < temp_max)
							{
								max = temp_max;
							}
							temp_max = a[i + 2];
							i += 2;
						}
					}
				}
				catch (std::out_of_range)
				{
					if (max < temp_max)
					{
						max = temp_max;
					}
				}
			}
		}
		std::cout << max << std::endl;
	}
	return 0;
}

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

using namespace std;

const int MAX_N=1e6,MAX_A=1e5;

typedef long long ll;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

int N;
int a[MAX_N];

ll sum[MAX_N+1];

int main(){
	do{
		scanf("%d",&N);
		if (N){
			REP(i,N){
				scanf("%d",&a[i]);
			}
			ll ma=-MAX_A;
			sum[0]=0;
			REP(i,N){
				sum[i+1]=sum[i]+a[i];
			}
			REP(i,N){
				FOR(j,i,N){
					ma=max(ma,sum[j+1]-sum[i]);
				}
			}
			printf("%lld\n",ma);
		}
	}while(N);
	return 0;
}

C++: #include <iostream>
#define TYPE long int
using namespace std;

int main(){
  int n;
  TYPE a[5010], s[5010];
  while(cin>>n && n){
    for( int i=1;i<=n;i++ )
      cin >> a[i];

    TYPE mx = a[1];
    for( int i=1;i<=n;i++ ){
      s[i] = a[i];
      mx = max(mx, s[i]);
      for( int j=i+1;j<=n;j++ ){
	s[j] = s[j-1] + a[j];
	mx = max(mx, s[j]);
      }
    }

    cout << mx << endl;
  }
  return 0;
}

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

using namespace std;

#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, m, n) for (int i = m; i <= n; ++i)

using i64 = long long;
using pii = pair<i64, i64>;

template<class A, class B>inline bool chmax(A &a, const B &b){return b > a ? a = b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b < a ? a = b,1 : 0;}

constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int MOD = int(1e9) + 7;

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

  int n;
  while (cin >> n, n) {

    vector<int> dp(n+5);
    int maxv = -INF;
    reps(i, 1, n) {
      int a;
      cin >> a;
      dp[i] = max(dp[i-1] + a, a);
      chmax(maxv, dp[i]);
    }

    cout << maxv << "\n";
  }

  return 0;
}

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

int main()
{
	int a;
	while(1){
		cin>>a;
		if(a==0){
			break;
		}
		int n[5000];
		for(int i=0;i<a;i++){
			cin>>n[i];
		}
		int max_ret = -1<<29;
		int dp[5000];
		dp[0] = n[0];
		for(int i=1;i<a;i++){
			if(dp[i-1]>0){
				dp[i] += dp[i-1] + n[i];
			}else{
				dp[i] = n[i];
			}
			max_ret = max( dp[i], max_ret);
		}
		
		cout<<max_ret<<endl;
	}
	
	
	return 0;
}