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;

int main(){
    
    for(int n; cin >> n, n;){
        vector<int>w(n);
        vector<vector<int> >dp(n, vector<int>(n, 0));
        for(int i = 0; i < n; i++)cin >> w[i];
        for(int l = 0; l + 1 < n; l++){
            if(abs(w[l] - w[l + 1]) <= 1){
                dp[l][l + 1] = 2;
            }
        }
        for(int i = 2; i <= n; i++){
            for(int l = 0; l + i < n; l++){
                int r = l + i;
                if(abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == r - l - 1){
                    dp[l][r] = r - l + 1;
                }
                for(int p = l; p < r; p++){
                    dp[l][r] = max(dp[l][r], dp[l][p] + dp[p + 1][r]);
                }
            }
        }
        cout << dp[0][n - 1] << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define REP(i,n) FOR(i,0,(n))

typedef long long ll;

const int M=10000000;
int dp[400][400];

vector<int>wass;
bool ok(int a, int b){
    return abs(wass[a]-wass[b])<=1;
}
int solve(int l,int r){
    if(dp[l][r]!=-1)return dp[l][r];
    if(l==r)return dp[l][r]=0;
    else if(l==r+1)return dp[l][r]=0;
    else{
        int ans=0;
        for(int p=l+1;p<r;++p){
            ans=max(ans,solve(l,p)+solve(p,r));
        }
        if(ok(l,r-1)){
            if(solve(l+1,r-1)==r-l-2){
                ans=r-l;
            }
        }
        return dp[l][r]=ans;
    }
}
int main()
{
    while(true){
        for(int i=0;i<400;++i){
            for(int j=0;j<400;++j){
                dp[i][j]=-1;
            }
        }
        int N;cin>>N;
        if(!N)break;
        wass.assign(N,0);

        for(int i=0;i<N;++i){
            cin>>wass[i];
        }
        int ans=solve(0,N);
        cout<<ans<<endl;
    }
   
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
#define MOD 1000000007
#define INF 1<<30
#define LINF (ll)1<<62
#define MAX 510000
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(),(x).end()
#define uni(q) unique(all(q)),q.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;



int main(){
    while(1){
        ll n; cin >> n;
        if(n==0) break;
        vl w(n);
        rep(i,n) cin >> w[i];
        ll dp[n+1][n+1] = {};
        for(ll W=2; W<=n; W++){
            rep(l,n){
                if(l + W > n) continue;
                ll r = l + W;
                if(dp[l+1][r-1] == W-2 && abs(w[l]-w[r-1])<=1){
                    dp[l][r] = W;
                }
                REP(mid,l,r+1){
                    dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
                }
            }
        }
        cout << dp[0][n] << endl;
    }
}