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; 

#define INF 1001000100010001000 
#define MOD 1000000007 
#define EPS 1e-10 
#define int long long 
#define rep(i, N) for (int i = 0; i < N; i++) 
#define Rep(i, N) for (int i = 1; i < N; i++) 
#define For(i, a, b) for (int i = (a); i < (b); i++) 
#define pb push_back 
#define eb emplace_back 
#define mp make_pair 
#define i_i pair<int, int> 
#define vi vector<int> 
#define vvi vector<vi > 
#define vb vector<bool> 
#define vvb vector<vb > 
#define vp vector< i_i > 
#define all(a) (a).begin(), (a).end() 
#define Int(x) int x; cin >> x; 
#define int2(x, y) Int(x); Int(y); 
#define int3(x, y, z) Int(x); int2(y, z); 
#define fir first #define sec second 
#define ffir first.first 
#define fsec first.second 
#define sfir second.first 
#define ssec second.second 
#define Decimal fixed << setprecision(10) 

//int dxy[5] = {0, 1, 0, -1, 0}; 

int n;
int data[300];
int dp[300][301]; // hankai
int score[300][301];

void make(int st, int en)
{
if (dp[st][en]) return;
dp[st][en] = 1;
if (0 <= st - 2 && abs(data[st-2] - data[st-1] ) < 2 ) {
make(st-2, en);
}
if (0 <= st - 1 && en < n && abs(data[st-1] - data[en]) < 2) {
make(st-1, en+1);
}
if (en + 1 < n && abs(data[en] - data[en+1]) < 2) {
make(st, en+2);
}
}

int solve(int st, int en)
{
if ( dp[st][en] ) return 0; 
if ( score[st][en] ) return score[st][en];
if (en - st < 2) return en - st;
int ret = en - st;
for (int i = st+2; i < en; i++) {
if (dp[st][i]) {
ret = min(solve(i-1, en), ret)
}
}
return score[st][en] = min(ret, 1+solve(st+1, en));
}

signed main() 
{ 
std::ios::sync_with_stdio(false); 
std::cin.tie(0); 

cin >> n;
while ( n ) {
memset(data, 0, sizeof(data))
memset(dp, 0, sizeof(dp));
rep(i, n) cin >> data[i];
rep(i, n-1) if ( abs(data[i] - data[i+1]) < 2 )
make(i, i+2);
cout << n - solve(0, n) <<endl;
cin >> n;
}

return 0; 
}

C++: #include <stdio.h>
#include <math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
using namespace std;




int main(){
    while(1){
        
        
        
        
    
    int n,a[500]={};
    cin>>n;
        if(n==0){break;}
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    bool j[500][500]={};
    for(int i=2;i<=n;i+=2){
        for(int t=0;t<=n-i;t++){
            if(abs(a[t]-a[t+i-1])<=1 && (j[t+1][t+i-2] || i==2)){j[t][t+i-1]=true; continue;}
            for(int h=t+1;h<t+i-1;h++){
                if(j[t][h] && j[h+1][t+i-1]){j[t][t+i-1]=true; break;}
            }
        }
    }
    int dp[500];
    fill(dp,dp+500,1000000000);
    dp[0]=1;
    for(int i=0;i<n;i++){
        if(j[0][i]){dp[i]=0;}
    }
    for(int i=1;i<n;i++){
        dp[i]=min(dp[i],dp[i-1]+1);
        for(int t=i;t<n;t++){
            if(j[i][t]){dp[t]=min(dp[t],dp[i-1]);}
        }
    }
    
    /*
    for(int i=0;i<n;i++){
        for(int t=0;t<n;t++){
            cout<<(j[i][t]?"1":"0")<<" ";
        }
        cout<<endl;
    }
     */
    
    //for(int i=0;i<n;i++){
        cout<<n-dp[n-1]<<endl;
    //}
    
    
        
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,b,e) for(int i=(b);i<(e);++i)
#define rep(i,e) for(int i=0;i<(e);++i)

int n, u, v;
int w[310];
int dp[310][310];

int main() {
  while(true){
    cin>>n;
    if(n==0) break;
    rep(i,n)w[i]=0;
    rep(i,n) cin>>w[i];

    rep(i,310)rep(j,310)dp[i][j] = 0;
    rep(i,n-1){
      if(abs(w[i+1]-w[i])<=1) dp[i][i+1]=2;
    }
    REP(k,3,n+1){
      rep(i,n-k+1){
        if(dp[i+1][i+k-1]>1 && abs(w[i]-w[i+k])<=1) dp[i][i+k]=k+1;
        rep(j,k){
          if(dp[i][i+j]>1 && dp[i+j+1][i+k]>1){
            dp[i][i+k]=k+1;
          }
        }
      }
    }
    rep(i,n)REP(j,i+1,n)REP(k,i,j){
      dp[i][j] = max(dp[i][j], dp[i][k]+dp[k+1][j]);
    }
    cout<<dp[0][n-1]<<endl;
  }
  return 0;
}

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

int n, w[300], dp[300][300], dp2[300][300];

int main() {

	while (true) {

		cin >> n;
		if (n == 0)break;

		for (int i = 0; i < n; i++) {
			cin >> w[i];
		}

		for (int i = 0; i < n - 1; i++) {
			if (abs(w[i] - w[i + 1]) <= 1) {
				dp[i][i + 1] = 2;
			}
		}

		for (int i = 3; i < n; i += 2) {//区間の大きさ
			for (int j = 0; j < n - i; j++) {
				//両端の計算
				if (abs(w[j] - w[j + i]) <= 1 && dp[j + 1][j + i - 1] > 0) {
					dp[j][j + i] = dp[j + 1][j + i - 1] + 2;
				}
				for (int k = j + 1; k < j + i; k += 2) {
					//jからkを中間とした長i+1区間を取り出せるかどうか

					if (dp[j][k] > 0 && dp[k + 1][j + i] > 0) {
						dp[j][j + i] = max(dp[j][j + i], dp[j][k] + dp[k + 1][j + i]);
					}
				}
			}
		}

		//区間での取り出せる個数の最大を計算
		for (int i = 1; i < n; i++) {
			for (int j = 0; j < n - i; j++) {
				//dpの方の取り出せる数を先に入れる
				dp2[j][j + i] = dp[j][j + i];

				for (int k = j; k < j + i; k++) {
					//jからkを中間とした長i+1区間の取り出せる個数の最大

					//ここが間違っている
					dp2[j][j + i] = max(dp2[j][j + i], dp2[j][k] + dp2[k + 1][j + i]);
					//dp2[j][j + i] = max(dp2[j][j + i], dp2[j][k] + dp[k + 1][j + i]);
					//dp2[j][j + i] = max(dp2[j][j + i], dp[j][k] + dp2[k + 1][j + i]);
					//dp2[j][j + i] = max(dp2[j][j + i], dp[j][k] + dp[k + 1][j + i]);
				}
			}
		}

		cout << dp2[0][n - 1] << endl;

		/*for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cout << dp[i][j] << " ";
			}
			cout << endl;
		}*/

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				dp[i][j] = dp2[i][j] = 0;
			}
		}
	}
}