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;
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);
	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;
	queue<pii> q;
	q.push({ 0,0 });
	while (!q.empty()) {
		pii p = q.front(); q.pop();
		if (p.fs == n) {
			cout << n-p.sc << endl;
			return;
		}
		rep(i, n) {
			if (dp[i][p.fs])q.push({ p.fs+i,p.sc});
		}
		q.push({ p.fs+1 ,p.sc + 1 });
	}

}

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

C++: #include <iostream>
#include <vector>
#include <cstring>
typedef long long ll;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, s, n) for(int i = s; i < n; i++)
using namespace std;

ll n;
ll block[301];
ll dp[301][301];
ll f(ll under, ll top){
  if (dp[under][top] != -1) return dp[under][top];
  if (top-under <= 1) return 0;
  ll res = 0;
  if (abs(block[under] - block[top-1]) <= 1 && f(under+1, top-1) == top-under-2){
    res = top - under;
  }
  rep2(middle, under+1, top){
    res = max(res, f(under, middle) + f(middle, top) );
  }
  return dp[under][top] = res;
}

int main(){
  while(1){
    cin >> n;
    if (n == 0) break;
    rep(i, n) cin >> block[i];
    memset(dp, -1, sizeof(dp));
    cout << f(0, n) << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int N;
int W[305];
int dp[305][305];
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0){break;}
		for(int i=0;i<N;i++){
			scanf("%d",&W[i]);
		}
		for(int i=0;i<305;i++){
			for(int j=0;j<305;j++){
				dp[i][j]=0;
			}
		}
		for(int i=0;i<=N-2;i++){
			if(abs(W[i]-W[i+1])<=1){dp[i][2]=2;}
		}
		for(int j=3;j<=N;j++){
			for(int i=0;i+j<=N;i++){
				//[i,i+j)
				for(int k=0;k<=j;k++){
					dp[i][j]=max(dp[i][k]+dp[i+k][j-k],dp[i][j]);
				}
				if(abs(W[i]-W[i+j-1])<=1){
					dp[i][j]=max(dp[i][j],dp[i+1][j-2]+2);
				}
			}
		}
		/*
		for(int j=0;j<12;j++){
			for(int i=0;i+j<=12;i++){
				printf("%d ",dp[i][j]);
			}
			printf("\n");
		}*/
		/*
		for(int i=150;i>=0;i-=2){
		printf("%d\n",dp[i][299-i]);
		}*/
		printf("%d\n",dp[0][N]);
	}
	return 0;
}

C++: #include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

//#define MAX 300
int n;
int* w;
vector<pair<int*,int>> wmap;

int* pop(int* w,int a) { //w???a????????¨a+1?????????????????????
	int *t;
	t = new int[n];
	for (int i = 0; i < n; i++) {
		if(i < a) t[i] = w[i];
		else t[i] = w[i + 2];
	}
	t[n - 2] = -1;
	t[n - 1] = -1;
	return t;
}


int daruma(int* w) {
	for (int i = 0; i < wmap.size(); i++) {
		if (memcmp(w, wmap[i].first, sizeof(w)) == 0) {
			return wmap[i].second;
		}
	}

	int max = 0;
	for (int i = 0; i < n - 1; i++) {
		if (w[i] != -1 && w[i + 1] != -1 && abs(w[i]-w[i+1])<=1) {
			int tmp = daruma(pop(w, i))+2;
			if (tmp > max) max = tmp;
		}
	}

	wmap.push_back(make_pair(w,max));
	return max;
}


int main() {
	cin >> n;
	while (n) {
		w = new int[n]();
		for (int i = 0; i < n; i++) {
			cin >> w[i];
		}
		cout << daruma(w) << endl;


		cin >> n;
	}


	return 0;
}

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

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
constexpr int MOD = 1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}

int dp[301][301];

int solve(vector<int> &W) {
    int N = W.size();
    for(int i = 0; i < 301; i++) {
        for(int j = 0; j < 301; j++) {
            dp[i][j] = 0;
        }
    }
    for(int i = 0; i <= N - 2; i++) {
        dp[i][i + 2] = 2 * (abs(W[i] - W[i + 1]) <= 1);
    }
    for(int d = 3; d <= N; d++) {
        for(int i = 0; i + d <= N; i++) {
            int j = i + d;
            int ma = (abs(W[i] - W[j - 1]) <= 1 && dp[i + 1][j - 1] == j - i - 2) ? j - i : 0;
            for(int k = i + 1; k < j; k++) {
                ma = max(ma, dp[i][k] + dp[k][j]);
            }
            dp[i][j] = ma;
        }
    }
    return dp[0][N];
}

void solve() {
    int N;
    while(cin >> N, N) {
        vector<int> W(N);
        for(int i = 0; i < N; i++) cin >> W[i];
        cout << solve(W) << endl;
    }
}

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}