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 <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
	int n;

	while(cin >> n, n) {
		vi num(n);

		REP(i, n) {
			cin >> num[i];
		}

		int ans = INT_MIN;
		FOR(i, 0, n-1) {
			int sum = 0;
			FOR(j, i, n-1) {
				sum += num[j];
				ans = max(sum, ans);
			}
		}

		cout << ans << endl;
	}

	return 0;
}

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

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

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

using namespace std;

int main(){
	int n,sumax,temp;
	int num[5001];
	
	while(scanf("%d",&n)){
		if(n==0)
		break;
		for(int i=0;i<n;i++){
			scanf("%d",&num[i]);
		}
		sumax = -100000;
		for(int i=0;i<n;i++){
			temp = 0;
			for(int j=i;j<n;j++){
				temp+=num[j];
				if(temp>sumax)
				sumax=temp;
			}
		}
		printf("%d\n",sumax);
	}
}

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

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

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

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 main()
 {
   int n;
   while(cin>>n&&(n!=0))
   {
     int sum=0;
     int max=0;
     for(int i=0;i<n;i++)
     {
       cin>>arr[i];
       if((sum+arr[i])>0)
       { 
         sum=sum+arr[i];
         if(sum>max) max=sum;
       }        
       else sum=0;
     }            
     cout<<max<<endl;         
   }
   //while(1);      
   return 0;    
 }

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int n;
ll a[6000];

ll sum(int i,int j){
	ll s=0;
	rep(k,j){
		s+=a[i+k];
	}
	return s;
}

int main(){
	while(cin>>n,n!=0){
		int c=0;
		ll MAX=-1000000000000000000;
		rep(i,n) a[i]=0;
		
		int cou=0;
		rep(i,n){
			cou++;
			cin>>a[0];
			if(a[0]!=0){
				break;
			}
			
		}
		n-=cou;
		rep(i,n){
			int t;
			cin>>t;
			if(a[c]>0&&t>0){
				a[c]+=t;
			}
			else if(a[c]*t==0){
			}
			
			else a[++c]=t;
		}
		
		
		c++;
		//rep(i,c) cout<<i<<" "<<a[i]<<endl;
		
		//rep(i,c) cin>>a[i];
		MAX=a[0];
		rep(i,c){
			ll sum=0;
			for(int j=i;j<c;j++){
				sum+=a[j];
				MAX=max(MAX,sum);
			}
		}
		
		
		
		/*
		for(int i=1;i<=c;i++){
			rep(j,c+1-i){
				MAX=max(MAX,sum(j,i));
			}
		}
		*/
		cout<<MAX<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n;
int a[50001];
int S[50001];


int main(){

while(cin >> n && n > 0){


  int max = -100000;
  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
    if( a[i] > max) max = a[i];
  }

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


  for (int i = 0; i < n + 1; ++i)
  {
    for (int k = 0; k < i ; ++k)
    {
      if(S[i] - S[k] > max) max = S[i] - S[k];
    }
  }

  cout << max << endl;

  }
}

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

int main(){
	int n;
	int a;
	int sum;
	int sum2;
	long int max;
	cin >> n;
	while(n){
		cin >> sum;
		max = sum;
		for(int i=0;i<n-1;i++){
			cin >> sum2;
			sum += sum2;
			if(sum < sum2){
				sum = sum2;
			}
			if(sum > max){
				max = sum;
			}
		}
		cout << max << endl;
		cin >> n;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
  int n,a;
  int i,j;
  int b[5005]={};

  while(1){
    priority_queue<int> pq;
    cin>>n;
    if(n==0)break;
    for(i=1;i<=n;i++){
      cin>>a;
      b[i]=b[i-1]+a;
      for(j=i-1;j>=0;j--)
	pq.push(b[i]-b[j]);
    }
    cout<<pq.top()<<endl;
  }
}

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

int main()
{
    while(1){
        int n;
        cin >> n;
        if( !n ) break;
        
        int a;
        int sum=0, max=0;
        for(int i=0; i<n; i++){
            cin >> a;
            
            if( a<=0 && sum<=0) continue;
            else sum += a;
            
            if( sum <= 0 ) {sum = 0; continue;}

            if( sum > max ) max = sum;
        }
        cout << max << endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>

typedef long long ll;
using namespace std;

int main()
{
	int T;
	int maxS=numeric_limits<int>::min();
	while(~scanf("%d",&T)){
		int* dp = new int[T+1]();
		if(T==0) break;
		for(int t=0;t<T;t++){
			int tmp;
			scanf("%d",&tmp);
			dp[t+1] = max(dp[t] + tmp,tmp);
			//printf("dp[%d+1] = %d,dp[%d] = %d\n",t,dp[t+1],t,dp[t]);
		}

		sort(dp,dp+T+1,greater<int>());
		cout << dp[0] << endl;

		delete[] dp;
	}
}

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

int main(void)
{
	while(1) {
		int n;
		long an[N], sum = 0, max = 0;
		cin >> n;

		if(n == 0)	break;

		for(int i = 0; i < n; i++) {
			cin >> an[i];
		}

		for(int i = 0; i < n; i++) {
			sum = 0;
			for(int j = i; j < n; j++) {
				sum += an[j];
				if(sum > max) {
					max = sum;
				}
			}
		}

		cout << max << endl;
	}
	return 0;
}