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 <algorithm>
using namespace std;

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

bool rec(int left, int right) {
    if(dp[left][right]) return true;
    if(left >= right) return false;

    bool ret = false;
    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 calc(int 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 ret;
}

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

        fill_n(*dp, 1000*1000, false);
        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>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int n; 
vector<int> w;
vector<vector<int>> dp;

int rec(int l = 0, int r = n){
    //[l,r)をみる
    if((r - l) <= 1) return 0;
    if((r - l) == 2){
        if(abs(w[l] - w[l+1])<=1) return 2;
        else return 0;
    }
    int &res = dp[l][r];
    if(res != -1) return res;
    if(abs(w[l] - w[r-1]) <= 1 && rec(l+1, r-1) == r - l - 2) chmax(res, r - l);//全部取り除ける時
    for(int i = l+1; i <= r-1; i++){
        chmax(res, rec(l,i) + rec(i, r));
    }
    return res;
}

int main(){
    vector<int> ans;
    while(true){
        cin >> n;
        if(n == 0) break;
        w.resize(n);
        for(int i = 0; i < n; i++){
            cin >> w[i];
        }
        dp.assign(n+1,vector<int>(n+1, -1));
        ans.push_back(rec());
    }
    for(auto i : ans) cout << i << endl;
    return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <numeric>
# include <tuple>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
#define ALL(x)    (x).begin(),(x).end()
#define UNIQ(c)   (c).erase(unique(ALL((c))), end((c)))
#define mp        make_pair
#define eb        emplace_back
typedef pair<LL, LL> P;
typedef pair<P, P> PP;

int N;
int W[333];

bool dp[333][333];
int dp2[333];
void solve() {
	for (int i = 0; i < N; i++)cin >> W[i];
	for (int i = 0; i < 333; i++)for (int j = 0; j < 333; j++)dp[i][j] = 0;
	for (int i = 0; i < 333; i++) dp2[i] = 0;
	for (int i = 0; i < N; i++)dp[i][i] = true;

	for (int len = 2; len <= N; len++) {
		for (int i = 0; i + len <= N; i++) {
			int j = i + len;
			if (abs(W[i] - W[j - 1]) <= 1)dp[i][j] |= dp[i + 1][j - 1];
			for (int k = i + 1; k < j; k++)dp[i][j] |= dp[i][k] & dp[k][j];
		}
	}
	for (int i = 0; i < N; i++) {
		dp2[i + 1] = max(dp2[i + 1], dp2[i]);
		for (int j = i + 1; j <= N; j++)if (dp[i][j])dp2[j] = max(dp2[j], dp2[i] + j - i);
	}
	cout << dp2[N] << endl;
}
int main() {
	while (cin >> N, N)solve();
}