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** maximum = new int*[301]; //?????????????????????????????§?¶????????????°
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		maximum[i] = new int[301];
	}

	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 = 1; k <= N; k++)maximum[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;
				maximum[i][i+1] = 2;
			}
		}

		//?¶??????????????????????????¨????
		for(int length = 4; length <= N; length += 2){ //??????????????????
			for(int left = 1; left+length -1 <= N; left++){ //??????????????????
				if((table[left][1] == true && table[left+2][(length-2)/2] == true) || //????????¨?????£???????¶????????????????????????????2????????£??????length-2??????????¶?????????´???
					(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)){ //??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???
					table[left][length/2] = true;
					maximum[left][left+length-1] = length; //????¨?????????§??????OK????????§?????¶????????§???????¨????
				}else{
					//maximum[left][left+length-1]?????´??°????????°????????????
					for(int i = 2; i <= length-2; i+= 2){
						maximum[left][left+length-1] = max(maximum[left][left+length-1],maximum[left][left+i-1]+maximum[left+i][left+length-1]);
					}
				}
			}
		}
		printf("%d\n",max(maximum[1][N-1],maximum[1][N]));
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U &u,const V... v){for(auto &e:u)fill_v<T>(e,v...);}

vector<int> w;
auto dp = make_vec<int>(0, 0);

int rec(int l, int r) {
    if (r - l == 1) return 0;
    if (r - l == 2) {
        if (abs(w[l] - w[r-1]) <= 1) return 2;
        else return 0;
    }
    if (dp[l][r] != -1) return dp[l][r];
    int &res = dp[l][r];
    if (rec(l+1, r-1) == r - l - 2 && abs(w[l] - w[r-1]) <= 1) res = r - l;
    else {
        for (int i = l+1; i < r; ++i) chmax(res, rec(l, i) + rec(i, r));
    }
    return res;
}

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

    int n;
    while (cin >> n, n) {
        w.resize(n);
        rep(i, n) cin >> w[i];
        dp.assign(n+1, vector<int>(n+1, -1));
        cout << rec(0, n) << endl;
    }

}

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

signed main(){
    // cout << fixed << setprecision(10) << flush;

    vector<int> ans;
    int n;
    cin >> n;
    while(n != 0){
        vector<int> w(n);
        for(int i=0; i<n; i++){
            cin >> w[i];
        }
    

        // 閉区間 [i, j] じゃなくて半開区間 [i, j) の方が便利そう？
        // dp[i][i] = 0, dp[i][i+1] = 1
        // 場合分け
        // どこかで二分割できて、それぞれの和になっている
        // dp[i][j] = min_{i+1 <= k <= j-1} (dp[i][k] + dp[k][j])
        // 真ん中がすっぽ抜けて、一番上と一番下が打ち消しあう
        // if(dp[i+1][j-1] == 0 && abs(w[i] - w[j-1])){
        //     dp[i][j] = 0;
        // }

        int INF = 1<<30;
        // dp[i][j] は [i, j) で残る個数の最小
        vector<vector<int>> dp(n+1, vector<int>(n+1, INF));
        for(int i=0; i<n; i++){
            dp[i][i] = 0;
            dp[i][i+1] = 1;
        }
        dp[n][n] = 0;

        for(int width=2; width<=n; width++){
            for(int i=0; i+width<=n; i++){
                int j = i + width;
                if(dp[i+1][j-1] == 0 && abs(w[i] - w[j-1]) <= 1){
                    dp[i][j] = 0;
                }
                else{
                    for(int k=i+1; k<=j-1; k++){
                        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
                    }
                }
            }
        }

        ans.push_back(n - dp[0][n]);
    
        cin >> n;
    }

    for(int i=0; i<ans.size(); i++){
        cout << ans[i] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#include <set>
#include <math.h>
#include <random>
#define PI 3.14159265359
#define ll long long
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define Rep(i, k, n) for(int i = k; i < (int)n; i++)
using namespace std;

int main(){
  while(true){
    int n;
    cin >> n;
    if (n == 0){break;}
    int w[n];
    rep(i, n){
      cin >> w[i];
    }
    int dp2[n][n];
    rep(i,n){
      rep(j,n){
        dp2[i][j] = 0;
      }    
    }
    
    Rep(i, 1, n) {
      rep(j, n-i) {
        if (dp2[j+1][j+i-1] == i-1 && abs(w[j]-w[j+i]) <= 1) {
          dp2[j][j+i] = dp2[j+1][j+i-1]+2;
        }
        rep(k, i) {
          dp2[j][j+i] = max(dp2[j][j+i], dp2[j][j+k] + dp2[j+k+1][j+i]);
        }
      }
    }
    cout << dp2[0][n-1] << endl;
  }
}