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 "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++) {
				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;
				}
				//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;
}

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

int main(){
    int n;
    cin >> n;
    while (n!=0){
        vector<int> w(n);
        for (int i = 0; i < n; i++){
            cin >> w[i];
        }
        vector<vector<bool>> check(n, vector<bool>(n));
        for (int i=0; i<n-1; i++){
            if (abs(w[i+1]-w[i])<=1) check[i][i+1]=true;
        }
        for (int i=3; i<n; i+=2){
            for (int j=0; j<n-i; j++){
                for (int k=j+1; k<j+i; k++){
                    if (!check[j][j+i] && check[j][k] && check[k+1][j+i]){
                        check[j][j+i]=true;
                        break;
                    }
                }
                if (!check[j][j+i] && abs(w[j]-w[j+i])<=1 && check[j+1][j+i-1]) check[j][j+i]=true;
            }
        }
        vector<int> dp(n);
        for (int i=0; i<n; i++){
            for (int j=0; j<n; j++){
                if (check[i][j]){
                    if (i!=0) dp[j]=max(dp[j],dp[i-1]+j-i+1);
                    else dp[j]=j-i+1;
                }
            }
            if (i!=0) dp[i]=max(dp[i],dp[i-1]);
        }
        cout << dp[n-1] << endl;
        cin >> n;
    }
}

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
int n;
int w[MAX];

string pop(string s, int a) {
	string t = s+"0";
	t[a] = '0';
	return t;
}

int daruma(string s) {
	if (s.length() == n) return 0;

	int tmp=0, tmp2=0;
	tmp = daruma(s + "1");
	if (s.rfind("1") != string::npos) {
		if (abs(w[s.length()] - w[s.rfind("1")]) <= 1) {
			tmp2 = daruma( pop(s, s.rfind("1") ) ) + 2;
		}
	}
	return max(tmp, tmp2);
}

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

		cout << daruma("1") << endl;
		
		cin >> n;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll n;
ll w[3010];
ll dp[3010][3010];
bool f[3010][3010];

int main() {
	while (cin >> n&&n) {
		rep(i, 301)rep(j, 301) {
			dp[i][j] = 0;
			f[i][j] = 0;
		}
		rep(i, n)cin >> w[i];

		for (int wid = 1; wid < n; wid++) {
			rep(i, n) {
				if (i + wid >= n)continue;
				if (wid == 1) {
					if (abs(w[i] - w[i + wid]) <= 1) {
						dp[i][i + wid] = 2;
						f[i][i + wid] = 1;
					}
				}
				else {
					if (wid % 2 == 1) {
						if (abs(w[i] - w[i + wid]) <= 1) {
							if (f[i + 1][i + wid - 1]) {
								dp[i][i + wid] = dp[i + 1][i + wid - 1] + 2;
								f[i][i + wid] = 1;
							}
						}
						dp[i][i + wid] = max(dp[i][i + wid], dp[i + 1][i + wid - 1]);
						for (int j = i + 1; j < i + wid; j++) {
							dp[i][i + wid] = max(dp[i][i + wid], (dp[i][j] + dp[j + 1][i + wid]));
							f[i][i + wid] |= (f[i][j] && f[j + 1][i + wid]);
						}
					}
					else {
						FOR(j, i, i + wid) {
							dp[i][i + wid] = max(dp[i][i + wid], dp[i][j] + dp[j + 1][i + wid]);
						}
					}
				}
			}
		}
		ll ans = 0;
		rep(i, n) {
			//ll sum = 0;
			FOR(j, i + 1, n) {
				//cout << i << " " << j << " " << dp[i][j] << endl;
				ans = max(ans, dp[i][j]);
				//if (dp[i][j])sum += max(ans, (ll)(j - i + 1));
			}
		}
		cout << ans << endl;
	}
}