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 <iostream>
#include <algorithm>
#include <string>

using namespace std;

int n;
int a[50000];
int S[50000];


int main(){

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

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

  int max = 0;
  for (int i = 0; i < n; ++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>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

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

		if ( !n )
		{
			break;
		}

		vector< LL > csum( 1, 0 );

		REP( i, 0, n )
		{
			int a;
			cin >> a;

			csum.PB( csum.back() + a );
		}

		LL res = LLONG_MIN;

		REP( i, 0, n )
		{
			REP( j, i + 1, n + 1 )
			{
				res = max( res, csum[j] - csum[i] );
			}
		}

		cout << res << endl;
	}

	return 0;
}

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

int main() {
    int n;

    while(cin >> n, n != 0) {
        n++;
        int a[5010]; a[0] = 0;
        for(int i=1; i<n; i++) {
            cin >> a[i];
            a[i] += a[i-1];
        }

        int ret = 0;
        for(int i=0; i<n; i++) {
            for(int j=i; j<n; j++) {
                ret = max(a[j] - a[i], ret);
            }
        }

        cout << ret << endl;
    }
    return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
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();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(long i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const LL INF = 1e18;

LL sum[5001];
int main(){
	int n;
	while(cin>>n){
		if(n==0)break;
		sum[0]=0;
		LL res=-INF;
		REP(i,n){
			int a;
			cin>>a;
			sum[i+1]=sum[i]+a;
		}
		
		REP(i,n+1)FOR(j,i+1,n+1){
			res=max(res,sum[j]-sum[i]);
		}
		cout<<res<<endl;
	}
	
	return 0;
}
/*

*/

C++: #include <vector>
#include <math.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;



int main()
{
	int n;
	while (cin >> n && n > 0)
	{
		vector<int> a = vector<int>(n);
		for (int i = 0; i < n; i++)
			cin >> a[i];

		vector<int> s = vector<int>(1, 0);
		bool f = true;
		for (int i = 0; i < n; i++)
		{
			if (a[i] >= 0)
			{
				if (!f)
					f = true;
				s[0] += a[i];
			}
			else if(s[0] > a[i])
			{
				if (f)
				{
					s.push_back(s[0]);
					f = false;
				}
				s[0] += a[i];
			}
			else
			{
				s[0] = 0;
			}

		}
		for (int i = 1; i < s.size(); i++)
		{
			if (s[i] > s[0])
				s[0] = s[i];
		}
		cout << s[0] << endl;
	}

	return 0;
}

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

int main() {
	int n;
	while (cin >> n, n) {
		int k[5003];
		for (int i = 0;i < n;i++)cin >> k[i];

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

		int ans = 1e-10;
		for (int i = 1;i <= n;i++)ans = max(ans, dp[i]);
		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n),n){
        int stage[100000] = {0};
        for(int i = 1;i <= n; i++){
            scanf("%d",&stage[i]);
        }
        int dp[100000]={0};
        dp[1] = stage[1];
        dp[2] = max(stage[2],dp[1]+stage[2]);
        int x = 0;
        int ans=dp[2];
        for(int i = 2;i <= n; i++){
            x = max(stage[i+1],stage[i+1]+dp[i]);
            if(x > ans)ans = x;
            dp[i+1] = x;
        }
        printf("%d\n",ans);
    }
    return 0;
}