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;
typedef long long ll;

int w[300];
int can[310][310];
int dp[310];

int len(int l, int r) {
	return r - l + 1;
}

int f(int l, int r) {
	if(can[l][r] != -1) return can[l][r];
	int L = len(l, r);
	if(L <= 2) {
		if(L == 0) {
			can[l][r] = 1;
			return 1;
		}
		if(L == 1) {
			can[l][r] = 0;
			return 0;
		}
		else {
			can[l][r] = abs(w[l] - w[r]) <= 1;
			return can[l][r];
		}
	}
	if(L % 2) {
		can[l][r] = 0;
		return 0;
	}
	if(abs(w[l] - w[r]) <= 1 && f(l + 1, r - 1)) {
		can[l][r] = 1;
		return 1;
	}
	for(int i = l + 1; i < r; i++) {
		if(f(l, i) && f(i + 1, r)) {
			can[l][r] = 1;
			return 1;
		}
	}
	can[l][r] = 0;
	return 0;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

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

		memset(can, -1, sizeof can);
		for(int l = 0; l < N; l++) {
			for(int r = l; r < N; r++) {
				f(l, r);
			}
		}

		memset(dp, 0, sizeof dp);
		for(int l = 0; l < N; l++) {
			dp[l + 1] = max(dp[l + 1], dp[l]);
			for(int r = l + 1; r < N; r++) {
				if(f(l, r)) {
					dp[r + 1] = max(dp[r + 1], dp[l] + len(l, r));
				}
			}
		}
		cout << dp[N] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep1(i,n) for(int i=1;i<=int(n);++i)
#define all(a) a.begin(),a.end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define INF 1e9

int dfs(int v, vector<set<int>> &g, vector<int> &dp){
    if(v == 0) return 0;
    if(dp[v]) return dp[v];
    // debug(g[v].size());
    int res = dfs(v-1,g,dp);
    for(int to : g[v]){
        res = max(res,v-to + dfs(to,g,dp));
    }
    // debug(v);
    // debug(res);
    return dp[v] = res;
}

int main(void){
    int n;
    while(cin>>n, n){
        vector<int> w(n+1);
        rep1(i,n) cin>>w[i];
        w[0] = INF;

        // g[i][j] := i番目が消える時に右側に会わせられる
        //             ブロックのindex
        vector<set<int>> g(n+1);
        rep1(i,n){
            if(abs(w[i]-w[i-1]) <= 1){
                g[i] = g[i-2];
                g[i].insert(i-2);
            }
            for(int l : g[i-1]){
                if(abs(w[i]-w[l]) <= 1){
                    for(int ll : g[l-1]){
                        g[i].insert(ll);
                    }
                    g[i].insert(l-1);
                }
            }
        }
        // rep1(i,n){
        //     printf("i = %d\n",i );
        //     for(int x : g[i]){
        //         cout<<x<<" ";
        //     }
        //     cout<<endl;
        // }
        vector<int> dp(n+1);
        cout<<dfs(n,g,dp)<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define REPW(i,n) for(i=0;i<(int)(n);++i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)   // c++14
#define SZ(c) ((int)c.size())
#define EXIST(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : " "); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,v) is >> * i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.first >> p.second; return is; }
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//------------------------------------------------------------------------------
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
    }
} before_main_function;
//------------------------------------------------------------------------------

int dp[301][301];
vi w;
int rec(int l,int r) {
    if(dp[l][r]!=INF) return dp[l][r];
    if(l==r) return dp[l][r]=0;
    int ret=0;
    REPF(i,l,r) {
        REPF(j,i+1,r) {
            if(abs(w[i]-w[j])<=1) {
                int tmp=0;
                tmp+=rec(l,i)+rec(j+1,r);
                if(rec(i+1,j)==j-i-1) {
                    tmp+=(j-i-1)+2;
                }
                chmax(ret,tmp);
            }
        }
    }
    return dp[l][r]=ret;
}
signed main() {
    while(1) {
        int n;
        cin>>n;
        if(n==0) break;
        REP(i,301) REP(j,301) dp[i][j]=INF;

        w=vi(n);
        cin>>w;
        cout<<rec(0,n)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
	
	int n;
	int w[500];
	while(true){
		cin >> n;
		if(n == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> w[i];
		}
		
		int DP[500][500] = {};
		
		for(int i = 2; i <= n; i += 2){
			for(int j = 0; j <= n - i; j++){
				DP[j][j + i - 1] = DP[j + 1][j + i - 2];
				if((w[j] - w[j + i - 1]) * (w[j] - w[j + i - 1]) <= 1 && DP[j + 1][j + i - 2] == (i - 2) / 2){
					DP[j][j + i - 1]++;
				}
				for(int k = j + 1; k < j + i - 1; k += 2){
					DP[j][j + i - 1] = max(DP[j][j + i - 1], DP[j][k] + DP[k + 1][j + i - 1]);
				}
			}
		}
		
		if(n % 2 == 1){
			cout << max(DP[0][n - 2], DP[1][n - 1]) * 2 << endl;
		}else{
			cout << DP[0][n - 1] * 2 << endl;
		}
	}
	return 0;
}