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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

int main(){

	int N,data[301];
	bool** table = new bool*[301]; //table[left][right] = left???????§?????????????2*right??????????????¨?¶??????§??????????????????
	int** dp = new int*[301]; //?????????????????????????????§?¶????????????°
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		dp[i] = new int[151];
	}

	bool FLG;

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		if(N <= 1){
			printf("0\n");
			continue;
		}

		//?¶??????????????????????????????????
		for(int i = 1; i <= N; i++){
			for(int k = 0; k <= N/2; k++)table[i][k] = false;
			for(int k = 0; k <= N; k++){
				dp[i][k] = 0;
			}
		}

		for(int i = 1; i <= N; i++)scanf("%d",&data[i]);

		//????????????2???????¨?
		for(int i = 1; i <= N-1; i++){
			if(abs(data[i+1]-data[i]) <= 1)table[i][1] = true;

		}

		//?¶??????????????????????????¨????
		for(int length = 4; length <= N; length += 2){ //??????????????????
			for(int left = 1; left+length -1 <= N; left++){ //??????????????????
				FLG = false;

				if(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)FLG = true;//??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???

				if(!FLG){
					for(int i = 2; i <= length-2; i += 2){
						if(table[left][i/2] == true && table[left+i][(length-i)/2] == true){
							FLG = true;
							break;
						}
					}
				}
				if(FLG){
					table[left][length/2] = true;
				}
			}
		}

		//?????§?¶????????????°????¨????
		for(int length = 2; length <= N; length += 2){ //??????????????????
			for(int left = N; left >= N-length+2; left--){
				dp[left][length/2] = max(dp[left][(length-2)/2],dp[min(N,left+1)][length/2]);
			}
			for(int left = N-length+1; left >= 1; left--){ //[??????]???N?????§??????[??????]????????????
				if(table[left][length/2] == true){ //left??????length?????????????¶?????????´???
					dp[left][length/2] = dp[min(N,left+length)][length/2]+length;
				}else{ //left??????length?????????????¶????????????´???
					dp[left][length/2] = dp[left+1][length/2];
					for(int i = 2; i <= length-2; i++){
						dp[left][length/2] = max(dp[left][length/2],max(dp[left][i/2] -dp[left+i][i/2] + dp[left+i][(length-i)/2],dp[left][(length-2)/2]));
					}
				}
				/*if(left == 1 && length == 10){
					printf("Minhi! length:%d ???dp[%d][%d]:%d????????????\n",length,min(N,left+length),length/2,dp[min(N,left+length)][length/2]);
				}
				printf("dp[%d][%d]:%d\n",left,length/2,dp[left][length/2]);*/
			}
		}

		printf("%d\n",dp[1][N/2]);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class T>
istream& operator >> (istream& is, vector<T>& v)
{
	for (auto &i : v) is >> i;
	return is;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& v)
{
	const string delimiter = "\n";
	REP(i, v.size())
	{
		os << v[i];
		if (i != v.size() - 1) os << delimiter;
	}
	return os;
}
/*--------------------template--------------------*/

int n;
vi w;
int dp[333][333];
vector<vector<bool>> del;

int del_all(int left, int right)
{
	if (dp[left][right] >= 0) return dp[left][right];
	int res = 0;
	if (left > right) res = 0;
	else if (left == right) res = 1;
	else
	{
		FOR(i, left + 1, right)
		{
			if (del_all(left, i) && del_all(i, right)) res = 1;
			if (abs(w[left] - w[right - 1]) <= 1 && del_all(left + 1, right - 1)) res = 1;
		}
	}
	return dp[left][right] = res;
}

int dp2[333];

int solve(int p)
{
	if (dp2[p] >= 0) return dp2[p];
	if (p == n) return 0;
	int res = 0;
	chmax(res, solve(p + 1));
	FOR(i, 1, n + 1)
	{
		if (del[p][i])
		{
			chmax(res, i - p + solve(i));
		}
	}
	return dp2[p] = res;
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	while (cin >> n, n)
	{
		MS(dp, -1);
		MS(dp2, -1);
		w.resize(n);
		REP(i, n) cin >> w[i];
		int ans = 0;
		del.clear();
		del.resize(n + 1, vector<bool>(n + 1));
		REP(i, n + 1)REP(j, i)
		{
			if (del_all(j, i))
			{
				del[j][i] = true;
			}
		}
		cout << solve(0) << endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#define REP(i,n,N) for(int i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int inf=1e9;

int dp[310][310];
int n;
int w[310];
int main() {
    while(cin>>n, n){
        REP(i,0,n) REP(j,0,n) dp[i][j] = 0;
        REP(i,0,n) cin>>w[i];
        for(int k=1;k<n;k++){
            REP(L,0,n-k){
                int R = L + k;
                if(dp[L+1][R-1] == k-1) dp[L][R] = max(dp[L][R], dp[L+1][R-1] + (abs(w[L]-w[R]) <= 1)*2);
                dp[L][R] = max({dp[L][R], dp[L][L+1] + dp[L+2][R], dp[L][R-2] + dp[R-1][R]});
            }
        }
        p(dp[0][n-1]);
    }

    return 0;
}