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>
#define rep(i,n) for(int i=0; i<(n); i++)
#define X first
#define Y second

using namespace std;
typedef pair<int,int> pii;

int main(){
	int n, w[1004];
	while(cin >> n, n){

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

		bool dp1[305][305]={};
		vector<pii> v;
		rep(i,n-1) if( abs(w[i] - w[i+1]) < 2 ){
			dp1[i][i+1] = true;
			v.emplace_back(i, i+1);
		}
		rep(k,n){
			rep(i,n){
				if( i+k >= n ) break;
				if( dp1[i][i+k] && 0 < i && i+k+1 < n ){
					if( abs(w[i-1] - w[i+k+1]) < 2 ){
						dp1[i-1][i+k+1] = true;
						v.emplace_back(i-1, i+k+1);
					}
				}
			}
			rep(j,n) rep(i,n){
				if( i+k+j >= n ) break;
				if( dp1[i][i+k] && dp1[i+k+1][i+k+j] ){
					dp1[i][i+k+j] = true;
					v.emplace_back(i, i+k+j);
				}
			}
		}

		//for(auto t: v) cout << t.X << "," << t.Y << " "; cout << endl;
		sort( v.begin(), v.end() );
		v.erase(unique(v.begin(), v.end()), v.end());

		int dp[900005] = {}, pos=0;
		rep(i,n) rep(k,n){
			if( i+k >= n ) break;
			if( dp1[i][i+k] ){
				dp[i+k+1] = max( dp[i+k+1], dp[i] + k+1 );
			}
			dp[i+1] = max( dp[i+1], dp[i] );
		}
		cout << dp[n] << endl;
//		rep(i,900002){
//			while( pos < v.size() && v[pos].first == i ){
//				int d  = v[pos].X;
//				int nx = v[pos].Y + 1;
//				dp[nx] = max( dp[nx], dp[i] + nx - d );
//				pos++;
//			}
//			dp[i+1] = max( dp[i+1], dp[i] );
//		}
//		cout << dp[900002] << endl;
	}
	return 0;
}

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

int main(){
  int n;
  while(cin >> n, n){
    int w[n];
    for(int i = 0; i < n; ++i)
      cin >> w[i];
    
    int dp[n][n];
    for(int i = 0; i < n; ++i){
      fill(dp[i],dp[i]+n,0);
    }
    
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
	if(j+i+1 > n-1) break;
	if(dp[j+1][j+i] == i && abs(w[j]-w[j+i+1]) < 2){
	  dp[j][j+i+1] = dp[j+1][j+i] + 2;
	}else{
	  for(int k = 0; k <= i; ++k){
	    dp[j][j+i+1] = max(dp[j][j+i+1],dp[j][j+k]+dp[j+k+1][j+i+1]);
	  }
	}
      }
    }
    //Debug
    /*
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
      cout << dp[i][j] << " ";
      }
      cout << endl;
    }

    */
    cout << dp[0][n-1] << endl;
  }
  return 0;
}

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

int dfs(int v, vector<set<int>> &g, vector<int> &dp){
    if(v == 0) return 0;
    if(dp[v]) return dp[v];
    int res = dfs(v-1,g,dp);
    for(int to : g[v]){
        res = max(res,v-to + dfs(to,g,dp));
    }
    return dp[v] = res;
}

int main(void){
    int n;
    while(cin>>n, n){
        vector<int> w(n+1);
        rep1(i,n) cin>>w[i];
        w[0] = -1;

        // g[i][j] := i番目が消える時に右側に会わせられる
        //             ブロックのindex
        vector<set<int>> g(n+1);
        rep1(i,n){
            if(abs(w[i]-w[i-1]) <= 1){
                g[i] = g[i-2];
                g[i].insert(i-2);
            }
            for(int l : g[i-1]){
                if(abs(w[i]-w[l]) <= 1){
                    for(int ll : g[l-1]){
                        g[i].insert(ll);
                    }
                    g[i].insert(l-1);
                }
            }
        }
        vector<int> dp(n+1);
        cout<<dfs(n,g,dp)<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

int n;
vector<int> w;
auto memo0 = make_vec<int>(330, 330);
int dp0(int l, int r) {
    if (memo0[l][r] != -1) return memo0[l][r];
    if (l + 1 == r) return memo0[l][r] = abs(w[l] - w[r]) <= 1;
    if (l + 1 < r - 1 && dp0(l + 1, r - 1) && abs(w[l] - w[r]) <= 1)
        return memo0[l][r] = 1;
    int ret = 0;
    for (int m = l + 1; m + 1 < r; m++) {
        ret |= (dp0(l, m) && dp0(m + 1, r));
    }
    return memo0[l][r] = ret;
}

auto memo1 = make_vec<int>(330, 330);
int dp1(int l, int r) {
    if (memo1[l][r] != -1) return memo1[l][r];
    if (dp0(l, r)) return memo1[l][r] = r - l + 1;
    int ret = 0;
    for (int m = l; m < r; m++) {
        chmax(ret, dp1(l, m) + dp1(m + 1, r));
    }
    return memo1[l][r] = ret;
}
int main() {
    while (1) {
        cin >> n;
        if (n == 0) break;
        w.resize(n);
        rep(i, n) cin >> w[i];
        rep(i, 330) rep(j, 330) memo0[i][j] = memo1[i][j] = -1;
        // rep(r, n) rep(l, r) cout << l << " " << r << " " << dp1(l, r) <<
        // endl;
        cout << dp1(0, n - 1) << endl;
    }
}