Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var infty = 100000 * 2
var scanner = bufio.NewScanner(os.Stdin)

func nextInt() (int, error) {
	scanner.Scan()
	return strconv.Atoi(scanner.Text())
}

func findMaximumSubarray(arr []int) int {
	currentMaxSum, maxSumContainsTail := -infty, -infty

	for j := range arr {
		if arr[j] > maxSumContainsTail+arr[j] {
			maxSumContainsTail = arr[j]
		} else {
			maxSumContainsTail += arr[j]
		}
		if currentMaxSum < maxSumContainsTail {
			currentMaxSum = maxSumContainsTail
		}

		// fmt.Println(arr[j], currentMaxSum, maxSumContainsTail)
	}
	return currentMaxSum
}

func main() {
	scanner.Split(bufio.ScanWords)
	for {
		size, _ := nextInt()
		if size == 0 {
			break
		}

		arr := make([]int, size)
		for i := range arr {
			n, _ := nextInt()
			arr[i] = n
		}

		maximumSum := findMaximumSubarray(arr)
		fmt.Println(maximumSum)
	}
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	int n, T[5001], dp[5001];
	while(cin >> n, n)
	{
		for(int i = 0; i < n; i++)
		{
			cin >> T[i];
		}

		memset(dp, -1, sizeof(dp));

		dp[0] = T[0];

		for(int i = 1; i < n; i++)
		{
			dp[i] = max(dp[i-1] + T[i], T[i]);
		}

		int res = -10000000;
		
		for(int i = 0; i < n; i++)
		{
			res = max(res, dp[i]);
		}

		cout << res << endl;
	}
}

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


int main()
{
    for (int n; cin >> n && n;) {
        vector<int> sequence(n);
        int sum = 0;
        int min = 0;
        int min_buffer = 0;
        int max = numeric_limits<int>::min();
        for (int x, i = 0; i < n && cin >> x; i++) {
            sum += x;
            if (sum < min)
                min_buffer = sum;
            if (sum > max) {
                max = sum;
                min = min_buffer;
            }
        }
        cout << max - min << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <stack>

using namespace std;

stack<int> s;
int ac;

int main(void) {
    while(~scanf("%d", &ac)) {
        if(ac == 0) {
            cout << s.top() << endl;
            s.pop();
        } else {
            s.push(ac);
        }
    }
    return 0;
}

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

int main(){
    long long int n, a[5001], ans;
    
    while(cin >> n){
        if(n == 0){
            break;
        }
        
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }
        
        
        for(int i = 0;i < n;i++){
            long long int sum = 0;
            for(int j = i;j < n;j++){
                sum += a[j];
                ans = max(sum, ans);
            }
        }
        
        cout << ans << endl;
        
    }
    
    
    return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n&&n!=0)
{
int a[n];
int A=0;
int R=-100000;
for(int h=0;h<n;h++)
{
	cin>>a[h];
}
for(int i=0;i<n;i++)
{
	for(int j=0;j<n-i-1;j++)
	{
	for(int k=i;k<=j;k++)
	{
		A=A+a[k];
		
	}	
	if(R<A)
	{
		R=A;
	}
	A=0;
	}
}
cout<<R<<endl;
}
return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;

int main() {
	int n;
	int ans;
	while (cin >> n&&n) {
		vector<int> p(n);
		for (int i = 0; i < n; i++) {
			cin >> p[i];
		}
		ans = p[0];
		for (int i = 0; i < n; i++) {
			int num = 0;
			for (int j = i; j < n; j++) {
				num += p[j];
				ans = max(ans, num);
			}
		}
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <sstream>
#include <utility>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <fstream>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef vector<pair<int, int> > vpii;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define dbg(x) cout << #x"="<< (x) << endl
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a) 
#define in(x) cin >> x;
#define all(x) (x).begin(), (x).end()
#define INF 2147483600
#define fi first
#define se second
using namespace std;
int sum[5001];
int main(){
	int N,ans;
	while(cin>>N&&N){
		ans = INT_MIN;
		rep(i,N){
			cin>>sum[i];
			sum[i] += sum[i-1];
		}
		rep(i,N){
			FOR(j,i,N){
				ans = max(ans,sum[j]-sum[i-1]);
			}
		}
		cout<< ans <<endl;
	}
}

C++: #include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main(){
  

  int dataSets;

  while(true){
    vector<int> a;
    cin >> dataSets;
    if(dataSets==0){break;}
    for(int i=0;i<dataSets;i++){
      int tmpa;
      cin >> tmpa;
      a.push_back(tmpa);

    }

    int tmax=0;
    // i?????§????????§?????????????????????
    vector<int> tmpMax(dataSets + 1,0);
    tmpMax[0] = 0;
    for(int i=1;i<dataSets+1;i++){
      tmpMax[i] += tmpMax[i-1] + a[i-1];
    }

    for(int i=0;i<dataSets+1;i++){
      for(int j=i;j<dataSets;j++){
	tmax = max(tmax, tmpMax[j+1] - tmpMax[i]);
      }
    }
    
    cout << tmax << endl;
    
  }

  return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)

int n, a[5000], s, r;

int main() {
	while (true) {
		scanf("%d",&n);
		if (n==0) break;
		r = -1001001001;
		rep(i,n) scanf("%d",&a[i]);
		rep(i,n) {
			s = 0;
			for (int j = i; j < n; ++j) {
				s += a[j];
				r = max(r, s);
			}
		}
		printf("%d\n",r);
	}
}

C++: #include <iostream>

using namespace std;

int main(){
  int n;
  while(true){
    cin>>n;
    if(!n)break;
    int seq[n];
    for(int i=0;i<n;i++)
      cin>>seq[i];
    int max = 0;
    for(int i=0;i<n;i++){
      for(int j=i;j<n;j++){
	int sum = 0;
	for(int k=i;k<=j;k++)
	  sum+=seq[k];
	if(sum>max)max = sum;
      }
    }
    cout << max << endl;
  }
  return 0;
}