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<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; }
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 2; i <= n; i++) {
			for (int j = 0; j < n; j++) {
				int a1 = j, a2 = i + j - 1; if (a2 >= n)continue;
				if (dp[a1][a2] == 0)continue;
				if (a2 < n - 2 && abs(a[a2 + 1] - a[a2 + 2]) <= 1)dp[a1][a2 + 2] = 1;
				if (a1 > 1 && abs(a[a1 - 1] - a[a1 - 2]) <= 1)dp[a1 - 2][a2] = 1;
				if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)dp[a1 - 1][a2 + 1] = 1;
			}
		}
		for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[i][j] == 1)dp[i][j] = j - i + 1; } }
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j <= n; j++) {
				for (int k = i; k <= j; k++) {
					int a1 = i, a2 = k, a3 = j; if (a3 > n)continue;
					dp[a1][a3 - 1] = max(dp[a1][a3 - 1], dp[a1][a2 - 1] + dp[a2][a3 - 1]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

inline int abs(int a, int b)
{
    return a<b ? b-a : a-b;
}

int main()
{
    int n;
    while(cin >> n, n)
    {
        int w[n];
        for(int i=0; i<n; i++) cin >> w[i];
        bool rm[n+1][n+1];
        for(int i=0; i<=n; i++)
            for(int j=0; j<=n; j++)
                rm[i][j] = i==j;
        for(int r=2; r<=n; r+=2)
        {
            for(int i=0; i+r<=n; i++)
            {
                for(int s=2; s<r; s+=2)
                {
                    if(rm[i][i+s] && rm[i+s][i+r]) rm[i][i+r] = true;
                }
                if(!rm[i][i+r] && rm[i+1][i+r-1] && abs(w[i], w[i+r-1])<=1)
                    rm[i][i+r] = true;
            }
        }
        /*
        int rmw = -1;
        for(int rmr = n / 2 * 2; rmw==-1 && rmr >= 0; rmr-=2)
        {
            for(int i=0; i+rmr<=n; i++)
            {
                if(rm[i][i+rmr])
                {
                    rmw = rmr;
                    break;
                }
            }
        }
        */
        int rmct[n+1][n+1];
        for(int y=0; y<=n; y++)
            for(int x=0; x<=n; x++)
                rmct[y][x] = rm[y][x] ? x - y : 0;
        for(int r=2; r<=n; r++)
        {
            for(int i=0; i<=n; i++)
            {
                for(int s=2; s<r; s++)
                {
                    rmct[i][i+r] = max(rmct[i][i+r], rmct[i][i+s]+rmct[i+s][i+r]);
                }
            }
        }
        cout << rmct[0][n] << endl;
        // XXX DEBUG
        /*
        printf("i\\j ");
        for(int x=0; x<=n; x++) printf("%3d ", x);
        cout << endl;
        for(int y=0; y<=n; y++)
        {
            printf("%3d ", y);
            for(int x=0; x<=n; x++)
            {
                printf("%3d ", rm[y][x]);
            }
            cout << endl;
        }
        */
        // XXX DEBUG
        //cout << rmw << endl;
    }
}

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

const long long mod = 1e9 + 7;

using ll = long long;
using pii  = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main(){
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        vi w(n);
        rep(i, n) cin >> w[i];
        vector<vi> dp(n, vi(n, 0));
        for (int wi=2; wi<=n; ++wi) rep(l, n) {
            int r = l + wi - 1;
            if (r >= n) continue;
            if (abs(w[l]-w[r])<=1&&dp[l+1][r-1]==wi-2) dp[l][r] = wi;
            else {
                for (int k=l+1; k<=r; ++k) {
                    dp[l][r] = max(dp[l][r], dp[l][k-1]+dp[k][r]);
                }
            }
        }
        cout << dp[0][n-1] << endl;
    }
    return 0;
}

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

int N;
vector<int> w;
int solve(){
    vector<vector<int>> dp(N+1, vector<int>(N+1, -1));
    for (int i = 0; i <= N; i++)
    {
        dp[i][i] = 0;
        if(i < N) dp[i][i+1] = 0;
    }

    for (int bet = 2; bet <= N; bet++)
    {
        for (int l = 0; l + bet <= N; l++)
        {
            int r = l + bet;
            if(dp[l+1][r-1] == bet - 2){
                if(abs(w[l] - w[r-1]) <= 1) dp[l][r] = bet;
                else dp[l][r] = bet - 2;
            }
            for (int mid = l+1; mid < r-1; mid++)
            {
                dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
            }
        }
    }
    return dp[0][N];
}

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