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 <bits/stdc++.h>
#include <iomanip>
#include <math.h>
//#include <unistd.h>
//#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const ld pi = 3.14159265358;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}

ll fac[510000], finv[510000], inv[510000];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < 510000; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);
ll N;
const ll MAXN = 305;
ll num[MAXN];
ll dp[MAXN][MAXN];
ll rec(ll l,ll r){
    if(dp[l][r] != -1)return dp[l][r];
    if(abs(l - r) <= 1)return 0;
    ll res = 0;
    if(abs(num[l] - num[r - 1]) <= 1 && rec(l + 1,r - 1) == r - l - 2){
        res = r - l;
    }
    for(ll i = l + 1;i <= r - 1;i++){
        res = max(res,rec(l,i) + rec(i,r));
    }
    return dp[l][r] = res;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(true){
        cin >> N;
        if(N == 0)break;
        rep(i,N)cin >> num[i];
        rep(i,N + 1){
            rep(j,N + 1)dp[i][j] = -1;
        }
        cout << rec(0,N) << endl;
    }
}

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

int DFS(vector<int> &w, vector<bool> f, int n, unordered_set<vector<bool>> &s) {
  int max = 0;
  for (int i = 0; i < n; i++) {
    if (f[i]) {
      for (int j = i + 1; j < n; j++) {
	if (f[j]) {
	  if (abs(w[i] - w[j]) <= 1) {
	    f[i] = f[j] = false;
	    if (s.find(f) == s.end()) {
	      s.emplace(f);
	      int ret = DFS(w, f, n, s) + 2;
	      if (ret > max) {
		max = ret;
	      }
	    }
	    f[i] = f[j] = true;
	  }
	  break;
	}
      }
    }
  }
  return max;
}

int main() {
  vector<int> w(300);

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

    if (n == 0) {
      break;
    }

    vector<bool> f(n);
    for (int i = 0; i < n; i++) {
      cin >> w[i];
      f[i] = true;
    }

    unordered_set<vector<bool>> s;
    cout << DFS(w, f, n, s) << endl;
  }

  return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

using namespace std;


int main() {
	ios::sync_with_stdio(false);
	list<int>res;
	cin >> N;
	while (N) {
		vector<int>num(N);
		for (int i = 0; i < N; i++)cin >> num[i];
		vector<vector<int>>dp(N, vector<int>(N,0));
		for (int i = 0; i < N - 1; i++) {
			if (abs(num[i] - num[i + 1]) < 2)dp[i][i + 1] = 2;
		}
		for (int i = 4; i <= N; i += 2) {
			for (int j = 0; i + j <= N; j++) {
				if (dp[j][i + j - 3] && dp[i + j - 2][i + j - 1]) {
					dp[j][i + j - 1] = i;
				}
				if (dp[j + 1][i + j - 2] && abs(num[j] - num[i + j - 1]) < 2) {
					dp[j][i + j - 1] = i;
				}
			}
		}
		vector<int>ret(N,0);
		ret[1] = dp[0][1];
		for (int i = 2; i < N; i++) {
			ret[i] = dp[0][i];
			for (int j = i - 1; j >= 1; j--) {
				ret[i] = max(ret[i], ret[j - 1] + dp[j][i]);
			}
		}
		res.push_back(ret[N - 1]);
		cin >> N;
	}
	for (auto i : res)cout << i << endl;
	return 0;
}