Go: package main

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

const (
	initialBufSize = 10000000
	maxBufSize     = 100000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	return int(a)
}
func nextString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextIntSlice(sc *bufio.Scanner, n int) (a []int) {
	a = make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt(sc)
	}
	return a
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[0]); j++ {
			dp[i][j] = -1
		}
	}
}

func resolve() {
	ans := []int{}
	for true {
		n := nextInt(sc)
		if n == 0 {
			break
		}
		as := nextIntSlice(sc, n)
		initDP(n)
		_rec(as, n, 0, n)
		ans = append(ans, dp[0][n])
	}
	for _, v := range ans {
		fmt.Println(v)
	}
	return
}

func _rec(as []int, n, l, r int) int {
	if dp[l][r] > -1 {
		return dp[l][r]
	}
	if r-l == 1 { // prevent to hit daruma
		dp[l][r] = 0
		return dp[l][r]
	}
	if r-l == 2 {
		if abs(as[l]-as[r-1]) <= 1 {
			dp[l][r] = 2
		} else {
			dp[l][r] = 0
		}
		return dp[l][r]
	}
	if abs(as[l]-as[r-1]) <= 1 {
		v := _rec(as, n, l+1, r-1)
		// if we can remove internal blocks
		if (r - 1 - (l + 1)) == v {
			dp[l][r] = v + 2
			return dp[l][r]
		} else {
			dp[l][r] = v
		}
	}
	for i := l + 1; i < r; i++ {
		v := _rec(as, n, l, i) + _rec(as, n, i, r)
		if dp[l][r] < v {
			dp[l][r] = v
		}
	}
	return dp[l][r]
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

C++: #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);

int DP[305][305];
int w[305];

int solve(int l, int r){
    if(DP[l][r]>-1) return DP[l][r];
    if(r<=l+1) return DP[l][r] = 0;
    if((abs(w[l]-w[r-1])<=1)&&(solve(l+1, r-1)==r-l-2)) return DP[l][r] = r-l;
    for(int i=1;l+i<=r-1;++i){
        DP[l][r] = max(DP[l][r], solve(l, l+i)+solve(l+i, r));
    }
    return DP[l][r];
}

int main() {
    int N;
    vector<int> ansarr;
    while(true){
        for(int i=0;i<305;++i){
            for(int j=0;j<305;++j){
                DP[i][j] = -1;
            }
        }
        cin>>N;
        if(N==0) break;
        for(int i=0;i<N;++i){
            cin>>w[i];
        }
        ansarr.push_back(solve(0, N));
    }
    for(int i=0;i<ansarr.size();++i){
        cout<<ansarr[i]<<endl;
    }
}

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

const int maxn = 300+5;
int n;
int mem[maxn];
int dp[maxn][maxn];

void Print()
{
	cout << "SHOW" << endl;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
		{
			if(j!=n-1) printf("%d ", dp[i][j]);
			else printf("%d\n", dp[i][j]);
		}
}

int solve()
{
	for(int i = 0; i + 1 < n; i++)
		if(abs(mem[i]-mem[i+1])<=1) dp[i][i+1] = 2;

	for(int i = 3; i <= n; i++)
		for(int s = 0; s+i <= n; s++)
		{
			int e = s + i - 1;
			if(abs(mem[s]-mem[e]) <= 1 && dp[s+1][e-1] == i-2)
				dp[s][e] = i;

			for(int k = s + 1; k <= e; k++)
				dp[s][e] = max(dp[s][e], dp[s][k-1]+dp[k][e]);
		}

	return dp[0][n-1];
}

void Init()
{
	memset(mem, 0, sizeof(mem));
	memset(dp, 0, sizeof(dp));
}

bool Input()
{
	scanf("%d", &n);
	if(!n) return false;
	Init();
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &mem[i]);
	}
	return true;
}

int main()
{
	while(1)
	{	
		if(!Input()) break;
		printf("%d\n", solve());
		//Print();
	}
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>
#include <deque>

using namespace std;

#define mod 1000000007

int main()
{
	while(1){
		int n;
		cin >> n;
		if(n == 0) break;
		int w[301];
		for(int i = 0; i < n; i++){
			cin >> w[i];
		}
		vector<vector<bool> > canerase(n + 1, vector<bool>(n + 1, false));
		// canerase[i][j] = i番目のやつから数えてj個(i番目を含む)を落とせるか
		for(int i = 0; i < n - 1; i++){
			canerase[i][2] = (abs(w[i] - w[i + 1]) <= 1);
		}
		for(int i = 4; i <= n; i++){
			for(int j = 0; i + j <= n; j++){
				canerase[j][i] = (abs(w[j] - w[i + j - 1]) <= 1) && canerase[j + 1][i - 2];
				for(int k = 2; k <= i; k++){
					canerase[j][i] = canerase[j][i] || (canerase[j][k] && canerase[j + k][i - k]);
					// cout << canerase[j][i] << endl;
				}
				// cout << j << " " << i << " " << (canerase[j][i]?1:2) << endl;
			}
		}
		int maxnum[301][301] = {};
		int ans = 0;
		for(int i = 2; i <= n; i++){
			for(int j = 0; i + j <= n; j++){
				if(canerase[j][i]) maxnum[j][i] = i;
				for(int k = 2; k <= i; k++){
					maxnum[j][i] = max(maxnum[j][i], maxnum[j][k] + maxnum[j + k][i - k]);
				}
				ans = max(ans, maxnum[j][i]);
			}
		}
		cout << ans << endl;
	}
}