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 <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(){
    int n,a[500]={};
    cin>>n;
    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,INT_MAX/2);
    dp[0]=0;
    for(int i=0;i<n;i++){
        if(i!=0){dp[i]=min(dp[i],dp[i-1]+1);}
        for(int t=i;t<n;t++){
            if(j[i][t]){dp[t+1]=min(dp[t+1],dp[i]);}
        }
    }
    
    /*
    for(int i=0;i<n;i++){
        for(int t=0;t<n;t++){
            cout<<(j[i][t]?"1":"0")<<" ";
        }
        cout<<endl;
    }
    */

        cout<<n-dp[n-1]-1<<endl;

    
    
    
    return 0;
}

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

using i64 = int64_t;

int dp[1005][1005]={0};

void init(){
    for(int i=0;i<=1000;++i){
        for(int j=0;j<=1000;++j)dp[i][j] = 0;
    }
}

int main(){
    int n;
    while(cin >> n, n != 0){
        init();
        vector<int> v;
        for(int i=0;i<n;++i){
            int w;
            cin >> w;
            v.push_back(w);
        }
        for(int i=0;i<n-1;++i){
            if(abs(v[i]-v[i+1]) <= 1)dp[i][i+1] = 2;
        }
        for(int d=3;d<n;d+=2){
            for(int i=0;i+d<n;++i){
                if(abs(v[i]-v[i+d])<=1 && dp[i+1][i+d-1] == d-1)dp[i][i+d] = max(dp[i][i+d], dp[i+1][i+d-1]+2);
                for(int j=i+1;j<i+d;++j){
                    dp[i][i+d] = max(dp[i][i+d], dp[i][j]+dp[j+1][i+d]);
                }
                dp[i][i+d] = max(dp[i][i+d], dp[i+1][i+d-1]);
                /*if(dp[i+1][i+d-1] == d-1 && abs(v[i]-v[i+d])<=1){
                    dp[i][i+d] = dp[i+1][i+d-1] + 2;
                }else{
                    bool ok = false;
                    for(int j=i+1;j<i+d;++j){
                        if(dp[i][j]+dp[j+1][i+d] == d+1){
                            dp[i][i+d] = d+1;
                            ok = true;
                            break;
                        }
                    }
                    if(!ok)dp[i][i+d] = dp[i+1][i+d-1];
                }*/
            }
        }
        int ans = 0;
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j)ans = max(ans, dp[i][j]);
        }
        cout << ans << endl;
    }

    return 0;
}