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>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
// #define int long long
using namespace std;
typedef pair<int, int> P;
 
int n;
int a[1010];
int dp[1010][1010];
int dp2[1010] = {};

//[l, r)
int f(int l, int r){
    if(dp[l][r] != -1) return dp[l][r];
    int res = 0;
    if(l == r) res = 1;
    for(int i = l + 2; i < r; i += 2){
        if(f(l, i) == 1 && f(i, r) == 1) res = 1;
    }
    // o(1);
    if(abs(a[l] - a[r - 1]) <= 1 && f(l + 1, r - 1) == 1) res = 1;
    dp[l][r] = res;
    return res;
}

signed main(){
    while(cin >> n && n){
        rep(i, 0, n) cin >> a[i];
        // rep(i, 0, 310) rep(j, 0, 310) dp[i][j] = -1;
        // rep(i, 0, 310) dp2[i] = 0;
        memset(dp, -1, sizeof(dp));
        memset(dp2, 0, sizeof(dp2));
        rep(i, 0, n){
            dp2[i + 1] = max(dp2[i], dp2[i + 1]);
            for(int j = i + 2; j <= n; j += 2){ 
                // o(f(i, j));               
                if(f(i, j) == 1){
                    dp2[j] = max(dp2[j], dp2[i] + j - i); 
                    // o(1);
                }               
            }
        }
        cout << dp2[n] << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int N, w[1000];
int dp[1001][1001];

int rec(int left, int right) {
    if(~dp[left][right]) return dp[left][right];
    if(left >= right) return 0;

    bool ret = 0;
    if(!(abs(left - right) % 2)) {
        rec(left + 1, right), rec(left, right - 1);
    } else {
        for(int i = left + 1; i < right; i += 2) {
            ret |= rec(left, i) & rec(i + 1, right);
        }
        ret |= rec(left + 1, right - 1) & (abs(w[left] - w[right]) <= 1);
    }
    return dp[left][right] = ret;
}

int maxi[1001];
int calc(int idx) {

    if(maxi[idx]) return maxi[idx];
    if(idx > N) return 0;

    int ret = 0;
    ret = calc(idx + 1);
    for(int i = idx + 1; i < N; i += 2) {
        if(dp[idx][i]) {
            ret = max(calc(i + 1) + (i - idx + 1), ret);
        }
    }
    return maxi[idx] = ret;
}

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

        fill_n(maxi, 1001, 0);
        fill_n(*dp, 1001*1001, -1);
        for(int i = 0; i < N - 1; i++) {
            if(abs(w[i] - w[i + 1]) <= 1) {
                dp[i][i + 1] = true;
            }
        }
        rec(0, N - 1);
        cout << calc(0) << endl;
    }
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int a[301],n,dp[301][301];
main(){
  while(cin>>n,n){
    int ans=0;
    r(i,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<n;i+=2)
      for(int j=0;j<n-i;j++){
        dp[j][j+i]=dp[j+1][j+i-1];
        if(abs(a[j]-a[j+i])<2&&dp[j+1][j+i-1]==i-1)dp[j][j+i]=max(dp[j][j+i],i+1);
        else for(int k=1;k<i;k+=2)dp[j][j+i]=max(dp[j][j+i],dp[j][j+k]+dp[j+k+1][j+i]);
      }
    r(i,n)r(j,n)ans=max(ans,dp[i][j]);
    cout<<ans<<endl;
  }
}

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

vector<int> W;
vector<vector<int>> dp;
// -1 : 未探索
// 0  : 全消し不可能
// 1  : 全消し可能

// [L, R)を全消しできるか？
bool rec(int L, int R) {
	int& ret = dp[L][R];
	if (ret >= 0) return ret;

	if (L == R) return ret = 1;
	if ((R - L) & 1) return ret = 0;

	ret = 0;
	for(int i = L + 1; i < R; i += 2) {
		if (abs(W[L] - W[i]) > 1) continue;
		if (rec(L + 1, i) && rec(i + 1, R)) return ret = 1;
	}
	return ret;
}

bool solve() {
	int N;
	cin >> N;
	if (N == 0) return false;

	W.resize(N);
	for(auto& w : W) cin >> w;

	dp.resize(N + 1);
	for(auto& v : dp) {
		v.assign(N + 1, -1);
	}

	vector<int> ans(N + 1, -1);
	ans[0] = 0;
	for(int L = 0; L <= N; ++L) {
		if (L > 0) ans[L] = max(ans[L], ans[L - 1]);
		for(int len = 2; L + len <= N; ++len) {
			if (rec(L, L + len)) ans[L + len] = max(ans[L + len], ans[L] + len);
		}
	}

	cout << ans[N] << endl;
	return true;
}

int main() {
	while (solve());
	return 0;
}

C++: #include<bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int> >
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vectors<decltype(inner)>(x, inner); }
using ll = long long;
using ld =long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int W,H;
bool inrange(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
using namespace std;
typedef pair<int, int> pii;
typedef pair<int,pii> piii;
#define MP make_pair
signed main(){
	int n;
	while(cin >> n,n){
		vector<vector<int>>dp(n,vector<int>(n,0));
		vector<int>array(n);
		rep(i,n) cin >> array[i];
		loop(k,2,n + 1)rep(i,n){
			int j = i + k - 1;
			if(j>=n)continue;
			if((abs(array[i]-array[j])<=1 && dp[i+1][j-1] + 2 == k)){
				dp[i][j] = k;
			}
			for(int l = i; l<j;l++){
				cmax(dp[i][j],dp[i][l] + dp[l+1][j]);
			}
		}
		cout << dp[0][n - 1] << endl;
	}
}