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++: // AOJ1611
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i,n) for(int i=0;i<int(n);i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ipair;
typedef pair<ll,ll> lpair;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v) //vectorの中身を見る
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

int solve(int n){
    vector<int> w(n+10);
    vector<vector<int>> dp(n+10,vector<int>(n+10,0));
    rep(i,n) cin>>w[i+1];
    //cout<<w<<endl;
    for(int i=1;i<=n-1;i++) if(abs(w[i]-w[i+1])<=1) dp[i][i+1]=2;
    for(int L=3;L<=n;L++){
        for(int i=1;i<=n-2;i++){
            int j=i+L-1;
            if(n<j) break;
            for(int k=i;k<=j-1;k++){
                dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
                if(abs(w[i]-w[j])<=1 && dp[i+1][j-1]==L-2) dp[i][j]=L;
            }
        }
    }
    return dp[1][n];
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    
    int x;
    while(cin>>x){
        if(x==0) break;
        cout<<solve(x)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>

using namespace std;

constexpr int MAX_N = 300;

int memo[MAX_N+1][MAX_N+1];
int w[MAX_N];

int dfs(int i, int j) {
    if(memo[i][j] != -1) return memo[i][j];

    // あるペアの差の絶対値が1以下で，ペアの間の数が全て消せていたら，
    // そのペアを含めた区間も全て消せる
    if(abs(w[i]-w[i+j-1]) <= 1 && dfs(i+1, j-2) == j-2) {
        return memo[i][j] = j;
    }

    // 区間を途中で区切って最大の場合を見つける
    for(int k=i+1;k<=i+j;++k) {
        memo[i][j] = max(memo[i][j], dfs(i, k-i) + dfs(k, i+j-k));
    }
    return memo[i][j];
}

int solve(int n) {
    for(int i=0;i<n;++i){
        cin >> w[i];
    }

    fill_n(memo[0], (MAX_N+1)*(MAX_N+1), -1);
    for(int i=0;i<=n;++i) {
        memo[i][0] = 0;
        memo[i][1] = 0;
    }
    
    return dfs(0, n);
}

int main() {
    int n;
    while(cin >> n && n) {
        cout << solve(n) << endl;
    }
}

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

int main() {
    int n;
    while (cin >> n, n) {
        vector<int> w(n);
        for (auto &wi: w) cin >> wi;
        vector<vector<int>> dp(n, vector<int>(n + 1));
        for (int len = 2; len <= n; len++) {
            for (int l = 0; l + len <= n; l++) {
                int r = l + len;
                if (dp[l + 1][r - 1] == len - 2 && abs(w[l] - w[r - 1]) <= 1) {
                    dp[l][r] = len;
                    continue;
                }
                for (int m = l; m < r; m++) {
                    dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
                }
            }
        }
        cout << dp[0][n] << endl;
    }
    return 0;
}