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 <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <bitset>
using namespace std;

vector<int> a(1);         //命令 a[1]-a[n] 1-n ->0-9,u
int ans;
void next(int z,vector<int> b);


int main(){
  int n;
  scanf ("%d",&n);
  while(n){
    ans=n;
    a.assign(n,0);
    for(int t=0;t<n;t++){
      scanf ("%d",&a[t]);
    }
    for(int t=0;t<int(a.size())-1;t++){
      if(-1<=(a[t]-a[t+1])&&(a[t]-a[t+1])<=1){
        next(t,a);
      }
    }
    
    ans=n-ans;
    printf("%d\n",ans);
    scanf ("%d",&n);
  }
}



void next(int z,vector<int> b){
  b.erase(b.begin()+z,b.begin()+z+2);
  //printf("%ld\n",b.size());
  if(int(b.size())<ans){
    ans=b.size();
  }
  for(int s=0;s<int(b.size())-1;s++){
    if(-1<=(b[s]-b[s+1])&&(b[s]-b[s+1])<=1){
      next(s,b);
    }
  }
}


//変化したところだけ見る

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

int remove(int a, int b){
	return (abs(a-b)<=1)? 2: 0;
}

int main(){
	while(1){
		int n;
		cin >> n;
		if(n==0) break;
		
		vector<int> w(n+1);
		for(int i=0; i<n; i++){
			cin >> w[i];
		}
		if(n%2!=0){
			w[n]=-1;
			n++;
		}
		
		vector<vector<int> > dp(n+1, vector<int>(n+1, 0));
		for(int i=2; i<=n; i+=2){
			for(int j=0; j+i<=n; j++){
				dp[j][i] = max(dp[j][i-2]+remove(w[j+i-2],w[j+i-1]), dp[j+2][i-2]+remove(w[j], w[j+1]));
				if(dp[j+1][i-2]==i-2){
					dp[j][i] = max(dp[j][i], i-2+remove(w[j], w[j+i-1]));
				}
			}
		}
		cout << dp[0][n] << endl;
	} 
	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];
bool dp[1000][1000];

int main() {
	while (cin >> n&&n) {
		rep(i, 1000)rep(j, 1000) {
			dp[i][j] = 0;
		}
		rep(i, n)cin >> x[i];
		for (int i = 1; i < n; i += 2) {
			for (int j = 0; j + i < n; j++) {
				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];
				}
			}
		}
		int ans = 0;
		rep(i, n) {
			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;
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
bool okay(int x, int y) { return abs(x-y)<=1; }
int main() {
    for (;;) {
        int N; cin >> N;
        if (N == 0) break;
        vector<int> W(N); for (auto &w : W) cin >> w;
        vector<vector<bool>> dp(N, vector<bool>(N));
        for (int c = 2; c <= N; c += 2) {
            for (int i = 0; i < N; i++) {
                int j = i + c - 1;
                if (j >= N) break;
                if (c == 2) {
                    dp[i][j] = okay(W[i], W[j]);
                } else {
                    if (okay(W[i], W[j]) && dp[i+1][j-1]) {
                        dp[i][j] = true;
                    }
                    for (int k = i + 2; k < j; k += 2) {
                        if (dp[i][k-1] && dp[k][j]) dp[i][j] = true;
                    }
                }
            }
        }
        vector<int> dp2(N);
        for (int j = 0; j < N; j++) {
            if (j > 0) dp2[j] = dp2[j-1];
            for (int i = 0; i < j; i++) {
                if (dp[i][j]) dp2[j] = max(dp2[j], (i==0?0:dp2[i-1]) + j-i+1);
            }
        }
        cout << dp2[N-1] << '\n';
    }
    return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 400


int main() {
	int a[N];
	f(i, N)a[i] = 0;
	int dp[N][N];
	int n, k;
	int x, y, z;
	int s, ans;
	bool v = true;
	while (true) {
		scanf("%d", &n);
		if (n == 0)return 0;
		f(i, N) {
			f(j, N)dp[i][j] = 0;
			a[i] = 0;
		}
		f(i, n) {
			scanf("%d", &a[i]);
		}
		f(j, n+1) {
			for(int i=j-1;i>=0;i--){
				dp[i][j] = dp[i][j - 1];
				k = 2;
				while ((j - k) >= i) {
					if (dp[j - k + 1][j - 1] == (k - 2) && abs(a[j - k] - a[j - 1]) <= 1) {
						dp[i][j] = max(dp[i][j], dp[i][j - k] + k);
					}
					k += 2;
				}
}
		}
		printf("%d\n", dp[0][n]);
	}



	return 0;
}