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<iomanip>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

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

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

int solve(){
    int n; cin>>n; 
    if(n==0) return -1;
    vector<int> v(n); 
    for(int i=0;i<n;i++){cin>>v[i];}
    vector<vector<int>> dp(n+1,vector<int>(n+1,0));
    for(int W = 2; W <= n; W++){ //区間の大きさ
        for(int l = 0; l < n; l++){
            int r = l + W;
            if(r > n) continue;
            if(W==2){
                if(abs(v[l]-v[r-1]) <= 1){dp[l][r]=2;}
                else dp[l][r] =0;
            }else{
                if(dp[l+1][r-1]==W-2 && abs(v[l]-v[r-1]) <= 1) dp[l][r] = W; //遷移1
                for(int m = l+1; m < r;m++) //遷移2
                dp[l][r] = max(dp[l][r],dp[l][m]+dp[m][r]);
            }
        }
    }
    return dp[0][n];
}
 
int main(){
    while(1){
        int ans=solve();
        if(ans==-1) break;
        cout << ans << endl;
    }
}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define all(x) x.begin(),x.end()
#define ln '\n'
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
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; }
///////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while (true) {
        int N; cin >> N;
        if (!N) break;
        vector<int> w(N);
        rep(i,N) cin >> w[i];

        vector<vector<int>> dp(N, vector<int>(N));
        for (int k = 1; k < N; k++) {
            rep(i,N-k) {
                if (abs(w[i]-w[i+k]) < 2 && dp[i+1][i+k-1] == k+1-2) dp[i][i+k] = dp[i+1][i+k-1]+2;
                else {
                    for (int j = 0; j < k; ++j) {
                        chmax(dp[i][i+k],dp[i][i+j]+dp[i+j+1][i+k]);
                    }
                }
            }
        }

        cout << dp[0][N-1] << ln;
    }
}