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 namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_N = 300;

int n;
int w[MAX_N];

bool dp[MAX_N][MAX_N];
int dp2[MAX_N][MAX_N];

int main() {
    while (true) {
        cin >> n;
        if (n == 0) break;
        REP(i, n) cin >> w[i];

        REP(i, n) {
            REP(j, n) dp[i][j] = false;
        }

        for (int i = 0; i+1 < n; i++) {
            if (abs(w[i] - w[i+1]) <= 1) dp[i][i+1] = true;
        }

        for (int i = 3; i < n; i += 2) {
            for (int j = 0; j + i < n; j++) {
                int k = j + i;
                if (dp[j+1][k-1] && abs(w[j] - w[k]) <= 1) dp[j][k] = true;
                for (int l = j + 2; l < k; l += 2) {
                    if (dp[j][l-1] && dp[l][k]) dp[j][k] = true;
                }
            }
        }

        int ans = 0;
        REP(i, n) {
            REP(j, n) dp2[i][j] = 0;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j + i < n; j++) {
                int k = j + i;
                if (dp[j][k]) dp2[j][k] = k - j + 1;
                for (int l = j; l < k; l++) dp2[j][k] = max(dp2[j][k], dp2[j][l] + dp2[l+1][k]);
            }
        }
        PRINT(dp2[0][n-1]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n;
int dp[500][500];
vector<int> w;

// [l,r)
int rec(int l, int r){
    if(dp[l][r] >= 0) return dp[l][r];
    if(r-l<2) return 0;

    int res = 0;
    res = max(res, rec(l,r-2)+(abs(w[r-2]-w[r-1])<=1?2:0));
    res = max(res, rec(l+2,r)+(abs(w[l]-w[l+1])<=1?2:0));
    if(rec(l+1,r-1) == r-l-2){
        res = max(res, r-l-2+(abs(w[l]-w[r-1])<=1?2:0));
    }
    else{
        res = max(res, rec(l+1,r-1));
    }
    res = max(res, rec(l,r-1));
    res = max(res, rec(l+1,r));
    return dp[l][r] = res;
}

int main() {
    while(cin >> n, n){
        w.resize(n);
        REP(i,n){
            cin >> w[i];
        }
        REP(i,n+1) fill(dp[i],dp[i]+n+1,-1);
        cout << rec(0,n) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF (ll)1E9
#define EPS 1E-9
#define MOD 1000000007
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}

//int dx[]={1,1,1,0,-1,-1,-1,0},dy[8]={1,0,-1,-1,-1,0,1,1,1};

//constexpr ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

void solve(int n) {
	vi v(n, 0);
	vi dist(n + 1, INF);
	vector<vector<bool>> dp(n + 1, vector<bool>(n, false));
	rep(i, n) {
		cin >> v[i];
	}
	for (int l = 2; l <= n; l += 2) {
		for (int i = 0; i + l <= n; i++) {
			bool b = false;
			if (l != 2) {
				for (int j = 2; j<l; j += 2) {
					if (dp[j][i] && dp[l - j][i + j])
						b = true;
				}
			}
			int dif = abs(v[i] - v[i + l - 1]);
			if (dif <= 1&&(l==2?1:dp[l-2][i+1]))b = true;
			if (b)dp[l][i] = true;
			//cout << l << " " << i << " " << dp[l][i] << endl;
		}
	}
	int pos = 0;
	deque<pii> q;
	q.push_back({ 0,0 });
	while (!q.empty()) {
		pii p = q.front(); q.pop_front();
		if (p.sc < dist[p.fs]) {
			dist[p.fs] = p.sc;
			if (p.fs == n) {
				cout << n - p.sc << endl;
				return;
			}
			for (int i = 2; i < n + 1; i += 2) {
				if (dp[i][p.fs]) {
					q.push_front({ p.fs + i,p.sc });
				}
			}
			q.push_back({ p.fs + 1 ,p.sc + 1 });
		}
	}

}

int main(void) {
	int n;
	while (cin >> n, n) {
		solve(n);
	}
}

C++: #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

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

int solve(int l, int r){
    if((r-l) <= 1) return 0;
    if((r-l) == 2){
        if(abs(w[l] - w[l+1]) <= 1) return 2;
        else return 0;
    }
    if(dp[l][r] != -1) return dp[l][r];

    if(abs(w[l] - w[r-1]) <= 1 && solve(l+1, r-1) == r-l-2) dp[l][r] = max(dp[l][r], r-l);
    for(int i = l+1; i < r; ++i) dp[l][r] = max(dp[l][r], solve(l, i)+solve(i, r));

    return dp[l][r];
}

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

    while(1){
        cin >> n;
        if(n == 0) break;

        w.resize(n);
        rep(i, n) cin >> w[i];

        dp.assign(n+2, vector<int>(n+2, -1));

        cout << solve(0, n) << endl;
    }

    return 0;
}