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 <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);
    }
    int max_elem;
    if ((max_elem = *max_element(seq.begin(), seq.end())) <= 0) {
      printf("%d\n", max_elem);
      continue;
    }
    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));
    }
    printf("%d\n", *max_element(partial_sums.begin(), partial_sums.end()));
  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
using namespace std;
int maxsum(int x,vector<int> a){
	
	vector<int> s(x+1,0);
	int max;
	for(int i=1;i<=x;i++){
		if(s[i-1]<0)
			s[i]=a[i];
		else
			s[i]=s[i-1]+a[i];
	}
	max = *max_element(s.begin(),s.end());
	return max;
	
}

int main(){
	
	int n;
	while(cin>>n,n!=0){
		
		vector<int> a(n+1,0);
		
		for(int i=1;i<=n;i++)
			cin>>a[i];
		
		cout<<maxsum(n,a)<<endl;
	}
	
 	
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>

using namespace std;

int main()
{
	int n;
	while(cin>>n,n){
		vector<long long> dp(n);
		long long res;
		cin>>dp[0];
		res=dp[0];

		for(int i=1;i<n;i++){
			long long a;
			cin>>a;
			dp[i]=max(dp[i-1]+a,a);
			res=max(res,dp[i]);
		}
		cout<<res<<endl;
	}


	return 0;
}

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

int main() {
  int x,n,k;
  int sum[100000],a[5000];
  while(cin >> n) {
    for(int i=0;i<n;i++) {
      cin >> x;
      a[i] = x;
    }
    for(int j=0;j<n;j++) {
      for(int i=j+1;j<n;i++) {
        for(int m=0;m<i-j;m++) {
          sum[k] += a[j+m];
        }
        k++;
      }
    }
    cout << *max_element(sum,sum+k) << endl;
  }
}

C++: #include <iostream>
#include <climits>
typedef long long ll;
using namespace std;
 
int main(){
    int n,a[5001];
    int ans;
    a[0]=0;
    while(cin>>n&&n){
        ans = INT_MIN;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            a[i] += a[i-1];
        }
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                ans = max(ans,a[j]-a[i-1]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

int main()
{
    int n;

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

        int max = 0, max2 = -1000000;
        int sum = 0;
        int a[n];
        for( int i = 0; i < n; i++ )
        {    
            cin >> a[i];

            if( a[i] > 0 )
            {
                sum += a[i];
                if( max < sum )
                    max = sum;
            }
            else
            {
                if( sum + a[i] > 0 )
                    sum += a[i];
                else
                    sum = 0;
            }
            
            if( max2 < a[i] )           //全てが負の値だった時に、その中でも最大の値を格納。
                max2 = a[i];
        }
        if( max == 0 )
            cout << max2 << endl;
        else
            cout << max << endl;
    }
    return 0;
}

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

constexpr int INF = 1e9;

int solve (int n) {
	int ans = -INF, sum = 0;
	for (int i = 0; i < n; i++) {
		int e; cin >> e;
		sum = max(sum + e, e);
		ans = max(ans, sum);
	}
	return ans;
}

int main() {
	int n;
	while (true) {
		cin >> n;
		if (n > 0) cout << solve(n) << '\n';
		else break;
	}
	return 0;
}

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

int main(){
	int n;
	while(cin >> n,n){
		int a,dp[5001] = {},ma = 0;
		for(int i = 0;i < n;i++) {
			cin >> a;
			dp[i + 1] = max(a,dp[i] + a);
			ma = max(ma,dp[i + 1]);
		}
		cout << ma << endl;
	}
	return 0;
}

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

int main()
{
	std::uint32_t n;
	for (std::cin >> n; n != 0; std::cin >> n) {
		typedef std::vector<std::int32_t> IntVec;
		IntVec a(n);
		for (std::size_t i = 0; i < n; ++i) {
			std::cin >> a[i];
		}
		for (std::size_t i = 1; i < n; ++i) {
			a[i] = std::max(a[i], a[i] + a[i - 1]);
		}
		std::cout << *std::max_element(a.begin(), a.end()) << std::endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

using namespace std;

int main()
{
	for (; ;)
	{
		int n;
		cin >> n;

		if (n == 0)
		{
			break;
		}

		vector<int> a(n);
		for (int i = 0; i < n; i++)
		{
			cin >> a.at(i);
		}

		int maxv = 0;
		for (int i = 0; i <= n; i++)
		{
			
			for (int j = 0; j < n; j++)
			{
				int sum = a.at(j);
				for (int k = j + 1; k < i; k++)
				{
					sum += a.at(k);
				}
				maxv = max(maxv, sum);
			}
		}

		cout << maxv << endl;
	}
}

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

int main(){
	int n;
	cin >> n;
	while(n != 0){	
		cout << n << endl;

		int a[n];
		int sum[n][n];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				sum[i][j] = 0;
			}
		}

		for(int i = 0; i < n; i++){
			cin >> a[i];
			cout << a[i] << endl;
		}
		int maximum = 0;
		

		for(int i = 0; i < n; i++){
			sum[i][0] = a[i];
			for(int j = 0; j < n - i; j++){
				sum[i][j+1] = sum[i][j] + a [i+j+1];
				maximum = max(maximum, sum[i][j]);
			}
		}
		cout << maximum << endl;
		cin >> n;
	}

	return 0;
}

C++: #include <iostream>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <utility>
using namespace std;
int main()
{
    int n;
	while(cin>>n&&n){
		int a[5000]={},sum[5000]={},m=-1000000;
		for(int i=0;i<n;i++){
			cin>>a[i];
			sum[i+1]=sum[i]+a[i];
		}
		for(int i=0;i<n;i++){//start
			for(int j=i;j<n;j++){//end
				int tmp=sum[j+1]-sum[i];
				if(tmp<=0){i=j;break;}
				//printf("%d-%d:%d\n",i,j,tmp);
				m=max(m,tmp);
			}
		}
		cout<<m<<endl;
	}
    return 0;
}