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 <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

const ll mod = 1e9 + 7;

int main() {
  ll n;
  while (true) {
    cin >> n;
    if (n == 0) break;

    vll ws(n);
    rep(i, n) cin >> ws[i];

    vvll dp(n, vll(n, 0));

    rep(i, n - 1) dp[i][i + 1] = 2 * (abs(ws[i] - ws[i + 1]) <= 1);

    for (ll h = 2; h <= n; h++) {
      for (ll i = 0; i < n - h + 1; i++) {
        ll k = i + h - 1;
        for (ll j = i; j <= k - 1; j++) {
          chmax(dp[i][k], dp[i][j] + dp[j+1][k]);
        }  // j
        bool cond = dp[i][k] == h && i >= 1 && k <= n - 2 &&
                    abs(ws[i - 1] - ws[k + 1]) <= 1;
        if (cond) {
          dp[i - 1][k + 1] = h + 2;
        }
      }  // i
    }    // h

    ll ans = dp[0][n - 1];
    cout << ans << endl;
  }  // while
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int x[100];
int dp[300][300];
//int ans[300];

//bool C(int lb, int ub) {
//	//cout << lb << " " << ub /*<< dp[lb][ub]*/ << endl;
//	if (dp[lb][ub] != -1)return dp[lb][ub];
//	if (ub - lb <= 0)return dp[ub][lb] = 0;
//	else if (ub - lb == 1) {
//		if (abs(x[lb] - x[ub]) <= 1)return dp[lb][ub] = 1;
//	}
//	dp[lb][ub] = 0;
//	if (abs(x[lb] - x[ub]) <= 1) {
//		dp[lb][ub] |= C(lb + 1, ub - 1);
//	}
//	FOR(mid, lb, ub) {
//		//cout << "!" << lb << " " << mid << endl;
//		//cout << "!" << mid << " " << ub << endl;
//		bool f = C(lb, mid);
//		f &= C(mid + 1, ub);
//		dp[lb][ub] |= f;
//	}
//	return dp[lb][ub];
//}

int main() {
	while (cin >> n&&n) {
		rep(i, 300)rep(j, 300) {
			dp[i][j] = 0;
		}
		rep(i, n)cin >> x[i];
		for (int i = 1; i < n; i += 2) {
			rep(j, n) {
				int lb = j, ub = j + i;
				if (abs(x[lb] - x[ub]) <= 1) {
					if (i == 1)dp[lb][ub] = 1;
					else dp[lb][ub] |= dp[lb + 1][ub - 1];
				}
				FOR(mid, lb + 1, ub - 1) {
					dp[lb][ub] |= dp[lb][mid] && dp[mid + 1][ub];
				}
			}
		}
		//rep(i, n) {
		//	FOR(j, i, n) {
		//		cout << i << " " << j << "  " << dp[i][j] << endl;
		//	}
		//}
		int ans = 0;
		rep(i, n) {
			//if (i >= 1)ans[i] = ans[i - 1];
			int num = 0;
			int pos = i;
			for (int j = i + 1; j < n; j += 2) {
				if (dp[i][j]) {
					num = max(num, j - i + 1);
					pos = j;
				}
			}
			ans += num;
			i = pos;
		}
		//rep(i, n) {
		//	FOR(j, i, n) {
		//		cout << i << " " << j << "  " << ans[i][j] << endl;
		//	}
		//}
		cout << ans << endl;
	}
}

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() );
		unique( v.begin(), v.end() );

		int dp[900005] = {}, pos=0;
		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;
}