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 = 1; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				int a1 = j, a3 = i + j; if (a3 >= n)continue;
				for (int k = a1 + 1; k <= a3; k++) {
					int a2 = k;
					dp[a1][a3] = max(dp[a1][a3], dp[a1][a2 - 1] + dp[a2][a3]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

// #define DEBUG

const int MAX_N = 300;
int main()
{
    ios::sync_with_stdio(false);

    int N;
    while (cin >> N, N) {
        vector<int> W(N);
        rep(i, N) cin >> W[i];

        bool dp[MAX_N][MAX_N] = {};
        rep(i, MAX_N) {
            dp[i][i] = true;
        }

        for (int len=2; len<=N; len+=2) {
            for (int l=0; l<=N-len; l++) {
                {
                    int inner_len = len - 2;
                    if (!(inner_len > 0 && !dp[l + 1][l + inner_len]) && abs(W[l] - W[l+ (len - 1)]) <= 1) {
                        dp[l][l + (len - 1)] = true;
                    }
                }
                for (int len2 = 2; len2 <= len - 2; len2+=2) {
                    int part_l = len2, part_r = len - len2;

                    if ((dp[l][l + part_l - 1] && dp[l + part_l][l + len - 1]) ||
                        (dp[l][l + part_r - 1] && dp[l + part_r][l + len - 1])) {
                        dp[l][l+len-1] = true;
                    }
                }
            }
        }

        int dp2[MAX_N + 1] = {};
        rep(r, N) {
            if (r > 0) dp2[r] = dp2[r-1];
            for (int len=2; len<=r+1; len+=2) {
                int l = r - len + 1;
#ifdef DEBUG
                // DUMP(len);
                // DUMP2(l, r);
#endif
                if (dp[l][r]) {
                    if (l-1 >= 0) {
                        dp2[r] = max(dp2[r], dp2[l-1] + len);
                    } else {
                        dp2[r] = max(dp2[r], len);
                    }
                }
            } 
        }

#ifdef DEBUG
        rep(l, N) {
            rep(r, N) cerr << dp[l][r] << " ";
            cerr << endl;
        }
        cerr << "-----------------------------" << endl;
        rep(i, N) cerr << dp2[i] << " ";
        cerr << endl;
#endif

        cout << dp2[N-1] << endl;
    }
}

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

int w[300];
int can[310][310];
int dp[310];

int len(int l, int r) {
	return r - l + 1;
}

int f(int l, int r) {
	if(can[l][r] != -1) return can[l][r];
	int L = len(l, r);
	if(L == 0) return 1;
	if(L == 2) return abs(w[l] - w[r]) <= 1;
	if(L % 2) return 0;

	if(abs(w[l] - w[r]) <= 1 && f(l + 1, r - 1)) {
		can[l][r] = 1;
		return 1;
	}
	for(int i = l + 1; i < r; i++) {
		if(f(l, i) && f(i + 1, r)) {
			can[l][r] = 1;
			return 1;
		}
	}
	can[l][r] = 0;
	return 0;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

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

		memset(can, -1, sizeof can);
		memset(dp, 0, sizeof dp);
		for(int l = 0; l < N; l++) {
			dp[l + 1] = max(dp[l + 1], dp[l]);
			for(int r = l + 1; r < N; r++) {
				if(f(l, r)) {
					dp[r + 1] = max(dp[r + 1], dp[l] + len(l, r));
				}
			}
		}
		cout << dp[N] << endl;
	}
}

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

#define reps(i,j,k) for(int i=j;i<k;i++)
#define rep(i,j) reps(i,0,j)
#define fs first
#define sc second
#define mk make_pair

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

int dp[305][305];
bool al[305][305]
;
int main(){
	int n;
	while(cin >> n, n){
		vi w(n);
		rep(i, n) cin >> w[i];
		fill(dp[0], dp[305], 0);
		fill(al[0], al[305], 0);
		reps(j, 2, n+1){
			rep(i, n - j + 2){
				int ij = i + j;
				if(j==2) al[i][ij] = abs(w[i]-w[i+1]) < 2;
				else{
					al[i][ij] = al[i+1][ij-1] && abs(w[i]-w[ij-1]) < 2;
					reps(k, i+1, ij){
						al[i][ij] = al[i][ij] ||  al[i][k] && al[k][ij];
					}
				}
				dp[i][ij] = al[i][ij] * j;
				rep(k, i) dp[k][ij] = max(dp[k][ij], dp[i][ij]);
				reps(k, ij+1, n+1) dp[i][k] = max(dp[i][k], dp[i][ij]);
			}
		
		}
		/*
		rep(i,n+1){
			rep(j,n+1){
				cout << al[i][j] << " ";
			}
			cout << endl;
		}*/

		rep(j, n+1){
			rep(i, n - j + 2){
				int ij = i + j;
				reps(k, i+1, j){
					dp[i][j] = max(dp[i][k] + dp[k][j], dp[i][j]);
				}

			}
		}
		cout << dp[0][n] << endl;
			

	}
	return 0;
}