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 LL = long long;
using namespace std;
int main(){
    while(1){
        int N;cin >> N;
        if(N != 0){
            vector<int> vec;
            for(int a = 0;a < N;a++){
                int b;cin >> b;
                vec.push_back(b);
            }
            vector<vector<int>> dp(N+1,vector<int>(N+1,-1));

            std::function<int(int,int)> calc = [&](int l, int r){
                if(dp[l][r] != -1)return dp[l][r];

                if(abs(l - r) <= 1)return 0;

                int ins = 0;

                if(abs(vec.at(l) - vec.at(r-1)) <= 1 && calc(l+1,r-1) == r - l -2){
                    ins = r - l;
                }
                for(int mid = l + 1 ;mid <= r -1 ;mid++){
                    ins = max(ins, calc(l,mid) + calc(mid, r));
                }
                return dp[l][r] = ins;
            };
            calc(0,N);
            cout<<dp[0][N]<<endl;
        }else{
            return 0;
        }
    }
}

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

constexpr int MAX_N = 300;
int n;
int w[MAX_N];
int memo[MAX_N][MAX_N + 1];

int dfs(const int l, const int r) {
	if(l >= r) return 0;
	int &res = memo[l][r];
	if(res != -1) return res;

	res = dfs(l + 1, r);
	for(int i = l + 1; i < r; i += 2) {
		const int width = i - l - 1;
		if(abs(w[l] - w[i]) <= 1 && dfs(l + 1, i) == width) {
			res = max(res, dfs(i + 1, r) + width + 2);
		}
	}
	return res;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	while(cin >> n && n) {
		for(int i = 0; i < n; ++i) {
			cin >> w[i];
		}
		memset(memo, -1, sizeof(memo));
		cout << dfs(0, n) << endl;
	}
	return EXIT_SUCCESS;
}

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];
	memset(dp, 0, sizeof(dp));
	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];
		}
	}
	memset(dp2, 0, sizeof(dp2));
	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();
}

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

int main() {
    int N;
    while (cin >> N, N) {
        vector<int> v(N);
        for (auto& i : v) cin >> i;

        vector<vector<int>> dp(N+1, vector<int>(N));
        for (int i = 0; i < N; i++) {
            if (abs(v[i] - v[i+1]) < 2) {
                dp[2][i] = 1;
            }
        }
        for (int i = 4; i <= N; i += 2) {
            for (int j = 0; j + i <= N; j++) {
                if (dp[i-2][j+1] && abs(v[j] - v[j + i - 1]) < 2) {
                    dp[i][j] = 1;
                } else {
                    for (int k = 2; j + k < j + i; k += 2) {
                        if (dp[k][j] && dp[i - k][j + k]) {
                            dp[i][j] = 1;
                        }
                    }
                }
            }
        }

        vector<int> sum(N+1);
        for (int i = 0; i < N; i++) {
            if (i) sum[i] = max(sum[i], sum[i-1]);
            for (int j = 2; j <= N; j += 2) {
                if (dp[j][i]) {
                    sum[i+j] = max(sum[i+j], sum[i] + j);
                }
            }
        }

        cout << max(sum[N-1], sum[N]) << endl;
    }

    return 0;
}