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<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++)
#define all(x) (x).begin(),(x).end()

using namespace std;
const int inf = 1001001000;

void print(vector<vector<int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

// int rec(vector<vector<int>> &dp, vector<int> &daruma, int i, int j){
// 	if (dp[i][j] =! -1)
// 		return dp[i][j];
// 	if (j - i <= 1)
// 		return dp[i][j] = 0;
// 	if (abs(daruma[i]-daruma[j-1]) <= 1 && rec(dp,daruma,i+1,j-1) == (j-1)-(i+1)){
// 		return dp[i][j] = j - i;
// 	}
// 	dp[i][j] = 0;
// 	rep2(mid, i + 1, j)
// 	{
// 		dp[i][j] = max(dp[i][j], rec(dp,daruma,i,mid) + rec(dp,daruma,mid,j));
// 	}
// 	return dp[i][j];
// }

int main(){
	while(true){
		int n;
		cin >> n;
		if (n == 0)
			break;
		vector<int> daruma(n);
		rep(i, n) cin >> daruma[i];
		vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
		// cout << rec(dp, daruma, 0, n) << endl;
		for(int W = 2;W <= n;W++)
		{
			for(int l = 0;l < n;l++)
			{
				int r = l + W;
				if(r > n) continue;

				if(dp[l + 1][r - 1] == W - 2 && abs(daruma[l] - daruma[r - 1]) <= 1)
					dp[l][r] = W;

				for(int mid = l;mid <= r;mid++)
				{
					dp[l][r] = max(dp[l][r] , dp[l][mid] + dp[mid][r]);
				}

			}

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

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl;
#define show2(a, n, m) rep(i, n) { rep(j, m) {cout << a[i][j] << ' ';} cout << endl;} 

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

int solve(vi& w) {
	int n = w.size();
	vector<vi> dp(n, vi(n));
	for(int d = 1; d <= n - 1; d++) {
		for(int l = 0; l + d < n; l++) {
			int r = l + d;
			if(d % 2 == 1 && dp[l + 1][r - 1] == d - 1 && abs(w[l] - w[r]) <= 1) {
				dp[l][r] = d + 1;
				continue;
			}
			for(int k = l; k + 1 <= r; k++) {
				dp[l][r] = max(dp[l][k] + dp[k + 1][r], dp[l][r]);
			}
		}
	}
//	show2(dp, n, n);
	return dp[0][n - 1];
}

int main() {
//	cout << fixed << setprecision(15);
	vi ans;
	while(true) {
		int n;
		cin >> n;
		if(n == 0) break;
		vi w(n);
		rep(i, n) cin >> w[i];
		ans.push_back(solve(w));
	}
	for(auto i : ans) cout << i << endl;
}

C++: #include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
using namespace std;
int func(vector<int> w){
    int s = w.size();
    if(s>1){
        int temp;
        int min = s;
        for(int i=0;i<s-1;i++){
            vector<int> remain;
            for(int j=0;j<s;j++){
                if(j==i){j+=2;}
                if(j>=s)break;
                remain.push_back(w[j]);
            }
            if(abs(w[i]-w[i+1])<2){
                temp = func(remain);
                min = min > temp ? temp : min;
            }
            if(min==0)break;
        }
        return min;
    } else {
        return s;
    }
}
void q_daruma(int n){
    vector<int> w;
    int t;
    for(int i=0;i<n;i++){cin>>t;w.push_back(t);}
    cout << w.size()-func(w) << "\n";
}
int main(){
    int n;
    while(cin >> n, n!=0){
        q_daruma(n);
    }
    return 0;
}

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

signed main(){
  int N;
  while(cin >> N, N){
    vector<int> a(N);
    vector<vector<int> > dp(N, vector<int> (N, 0));
    for(int i = 0; i < N; i++) cin >> a[i];
    for(int i = 1; i < N; i++){
      for(int j = 0; j + i < N; j++){
        for(int k = j; k < i + j; k++){
          dp[j][j + i] = max(dp[j][j + i], dp[j][k] + dp[k + 1][j + i]);
        }
        if(dp[j + 1][j + i - 1] == i - 1 && abs(a[j] - a[j + i]) <= 1){
          dp[j][j + i] = i + 1;
        }
      }
    }
    cout << dp[0][N - 1] << endl;
  }
}