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

	bool FLG;

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

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

		for(int i = 0; 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]);

		for(int i = 1; i <= N-1; i++){
			if(abs(data[i+1]-data[i]) <= 1){
				table[i][1] = true;
				dp[i][i+1] = 2;
			}else{
				dp[i][i+1] = 0;
			}
		}

		for(int length = 3; length <= N; length += 1){
			for(int left = 1; left+length -1 <= N; left++){

				if(length%2 == 0){ //??????????\???°?????´???
					FLG = false;

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

					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;
						dp[left][left+length-1] = length;
					}else{
						for(int i = left; i <= left+length-2;i++){
							dp[left][left+length-1] = max(dp[left][left+length-1],dp[left][i]+dp[i+1][left+length-1]);
						}
					}
				}else{
					for(int i = left; i <= left+length-2;i++){
						dp[left][left+length-1] = max(dp[left][left+length-1],dp[left][i]+dp[i+1][left+length-1]);
					}
				}
			}
		}

		if(N%2 == 0)printf("%d\n",dp[1][N]);
		else{
			printf("%d\n",max(dp[1][N-1],dp[2][N]));
		}
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;
const int INF=1<<17;

int dp[300][300]; //dp[i][j]:=[i,j)で取り除けるブロックの最大値
vector<int> W;

int rec(int l,int r){ //[r,l)
    if(~dp[l][r]) return dp[l][r];
    if(r-l<=1) return dp[l][r]=0;
    else if(r-l==2){
        if(abs(W[r-1]-W[l])<=1) return dp[l][r]=2;
        else dp[l][r]=0;
    }
    int ret=0;
    if(l+1<r-1&&abs(W[l]-W[r-1])<=1&&rec(l+1,r-1)==r-l-2){
        ret=max(ret,rec(l+1,r-1)+2);
        return dp[l][r]=ret;
    }
    for(int i=l+1;i<r;i++){
        ret=max(ret,rec(l,i)+rec(i,r));
    }
    return dp[l][r]=ret;
}


int main(){
    int n;
    while(cin >> n){
        if(!n) break; 
        W.clear();
        for(int i=0;i<n;i++){
            int w;
            cin >> w;
            W.push_back(w);
        }
        memset(dp,-1,sizeof(dp));
        cout << rec(0,n) << endl;
    }
    return 0;
}

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

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;
        vector<int> W(n);
        vector<vector<int>> dp(n, vector<int>(n, 0));
        for(int i = 0; i < n; i++) cin >> W[i];
    
        if(n == 1){
            cout << 0 << endl;
            return 0;
        }
    
        for(int w = 2; w <= n; w++){
            for(int r = 0; r <= n - w; r++){
                for(int k = r; k < r + w - 1; k++){
                    dp[r][r+w-1] = max(dp[r][r+w-1], dp[r][k]+dp[k+1][r+w-1]);
                }
                if(dp[r+1][r+w-2] == w-2 && abs(W[r]-W[r+w-1])<=1) dp[r][r+w-1] = max(dp[r][r+w-1], dp[r+1][r+w-2]+2);
            }
        }
    
        cout << dp[0][n-1] << endl;
    }
}

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

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'

// n回繰り返し
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順
#define rep1(i,n) for(int i = 1; i <= ((int)(n)); i++) // 1-indexed昇順
#define rrep(i,n) for(int i = ((int)(n)-1); i >= 0; i--)  // 0-indexed降順
#define rrep1(i,n) for(int i = ((int)(n)); i >= 1; i--)   // 1-indexed降順
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;  // 2次元配列
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> PII;

template<typename T>
using vec = vector<T>;
template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T,class U> inline bool chmax(T &a, const U &b){if(a<b){a=b;return 1;}return 0;}
template<class T,class U> inline bool chmin(T &a, const U &b){if(a>b){a=b;return 1;}return 0;}

const ld Pi = std::acos(-1.0L);
const ll infll = (1LL<<62)-1;
const int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;

vii dp;
vi w;
/*FUNCs=================================================*/


/*MAIN==================================================*/
signed main(){
    int _StartTime_ = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);   //cin cout 高速化
    // cout << fixed << setprecision(15);


    vii dp_init(310, vi(310));
    vi w_init(310);
    int n, p;
    while(cin >> n) {
        if(n==0) break;
        dp = dp_init;
        w = w_init;
        rep1(i,n) cin >> w[i];

        for(int l = 2; l <= n; ++l) {
            for(int i = 0; i+l <= n; ++i) {
                int j = i + l;
                if(dp[i+1][j-1] == (j-1)-(i+1)) {
                    if(abs(w[i+1]-w[j]) <= 1) chmax(dp[i][j], dp[i+1][j-1] + 2);
                }
                for(int k = i+1; k < j; ++k) {
                    chmax(dp[i][j], dp[i][k] + dp[k][j]);
                }
            }
        }
        cout << dp[0][n] << '\n';
    }

    // printf("ExecutionTime: %d /ms\n",1000*(int)((clock()-_StartTime_)/CLOCKS_PER_SEC));
}