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>

using namespace std;

#define int long long
#define MKP make_pair
typedef pair<int, int> pii;

constexpr int MAX_N = 305;
constexpr int INF = 1LL << 60;

int n, mem[MAX_N][MAX_N], w[MAX_N];

int solve(int l, int r) {
    if (r - l <= 1) {
        return 0;
    }
    if (mem[l][r] != -1) {
        return mem[l][r];
    }

    int res = 0;
    for (int i = l + 1; i < r; ++i) {
        res = max(res, solve(l, i) + solve(i, r));
    }
    for (int i = r - 1; l + 1 <= i; --i) {
        res = max(res, solve(l, i) + solve(i, r));
    }

    if (abs(w[l] - w[l + 1]) <= 1) {
        res = max(res, solve(l + 2, r) + 2);
    }
    if (abs(w[r - 1] - w[r - 2]) <= 1) {
        res = max(res, solve(l, r - 2) + 2);
    }
    if (abs(w[l] - w[r - 1]) <= 1) {
        int tmp = solve(l + 1, r - 1);
        if (tmp == r - l - 2) {
            res = max(res, tmp + 2);
        }
    }

    return mem[l][r] = res;
}

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

    while(1) {
        cin >> n;
        if (n == 0) {
            return 0;
        }
        memset(mem[0], -1, sizeof(mem));

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

        cout << solve(0, n) << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
vi in;
vvi dp;
int n;
int f(int a,int b){
	if(a<0||a>=n||b<=0||b>n)return 0;
	if(dp[a][b]!=-inf)return dp[a][b];
	int out=0;
	{
		int t=f(a+1,b-2);
		if(t==b-2&&abs(in[a]-in[a+b-1])<=1)out=t+2;
	}
	rep(i,b-1){
		int s=f(a,i+1);
		int t=f(a+i+1,b-i-1);
		out=max(out,s+t);
	}
	if(b==2&&abs(in[a]-in[a+1])<=1)out=2;
	return dp[a][b]=out;
}
int main(){
	while(cin>>n,n){
		in=vi(n);
		rep(i,n)cin>>in[i];
		dp=vvi(n,vi(n+1,-inf));
		cout<<f(0,n)<<endl;
//		rep(i,n){rep(j,n+1)cout<<" "<<dp[i][j];cout<<endl;}
//		cout<<endl;
	}
}

C++: #include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

//#define MAX 300
short n;
short* w;
vector<pair<short*,short> > wmap;

short* pop(short* w,short a) { //w???a????????¨a+1?????????????????????
	short *t;
	t = new short[n];
	for (short i = 0; i < n; i++) {
		if(i < a) t[i] = w[i];
		else t[i] = w[i + 2];
	}
	t[n - 2] = -1;
	t[n - 1] = -1;
	return t;
}

short daruma(short* w) {
	for (short i = 0; i < wmap.size(); i++) {
		if (memcmp(w, wmap[i].first, sizeof(w)) == 0) {
			return wmap[i].second;
		}
	}

	short max = 0;
	for (short i = 0; i < n - 1; i++) {
		if (w[i] != -1 && w[i + 1] != -1 && abs(w[i]-w[i+1])<=1) {
			short tmp = daruma(pop(w, i))+2;
			if (tmp > max) max = tmp;
		}
	}

	wmap.push_back(make_pair(w,max));
	return max;
}

int main() {
	cin >> n;
	while (n) {
		wmap.clear();
		w = new short[n]();
		for (short i = 0; i < n; i++) {
			cin >> w[i];
		}
		cout << daruma(w) << endl;
		cin >> n;
	}
	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);
	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++) {
				for (int k = j; k < i + j - 1; k+=2) {
					for (int l = k + 1; l <= i + j - 1; l+=2) {
						if (!k) {
							if (dp[k + 1][l - 1] == l - k - 1) {
								if (l == N - 1) {
									if (abs(num[k] - num[l]) < 2) {
										dp[j][i + j - 1] = i;
								//		cout << j << " " << i + j - 1 << " " << dp[j][i + j - 1] << endl;
									}
								}
								else if(dp[l + 1][i + j - 1] == i + j - 1 - l&&abs(num[k] - num[l]) < 2) {
									dp[j][i + j - 1] = i;
								//	cout << j << " " << i + j - 1 << " " << dp[j][i + j - 1] << endl;
								}
							}
						}
						else if (dp[j][k - 1] == k - j) {
							if (dp[k + 1][l - 1] == l - k - 1) {
								if (l == N - 1) {
									if (abs(num[k] - num[l]) < 2) {
										dp[j][i + j - 1] = i;
									//	cout << j << " " << i + j - 1 << " " << dp[j][i + j - 1] << endl;
									}
								}
								else if (dp[l + 1][i + j - 1] == i + j - 1 - l&&abs(num[k] - num[l]) < 2) {
									dp[j][i + j - 1] = i;
									//cout << j << " " << i + j - 1 << " " << dp[j][i + j - 1] << endl;
								}
							}
						}
					}
				}
			}
		}
		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]);
			}
		}
		cout << ret[N - 1] << endl;
		cin >> N;
	}
	return 0;
}