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 ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

//サイズが1のときだけ値を持っておくといいね．典型だねうれしいね
int dp[301][301];
int v[301][301];
bool check[301][301];
int n;
int w[301];

int DP(int l,int r){
    if(check[l][r]){
        return dp[l][r];
    }
    check[l][r]=true;
    if(l==r){
        v[l][r]=w[l];
        return dp[l][r]=1;
    }
    
    for(int i=l;i<r;i++){
        auto x=DP(l,i),y=DP(i+1,r);
        if(x==0 and y==1){
            if(chmin(dp[l][r],x+y)) v[l][r]=v[i+1][r];
        }
        else if(x==1 and y==0){
            if(chmin(dp[l][r],x+y)) v[l][r]=v[l][i];
        }
        else if(x==1 and y==1){
            if(abs(v[l][i]-v[i+1][r])<2) dp[l][r]=0;
            else chmin(dp[l][r],x+y);
        }
        else chmin(dp[l][r],x+y);
    }
    return dp[l][r];
}


void solve(){
    rep(i,n) cin>>w[i];
    rep(i,n)rep(j,n)check[i][j]=false,dp[i][j]=INF;
    cout<<n-DP(0,n-1)<<endl;
}

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

    while(cin>>n,n){
        solve();
    }
    return 0;
}

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

using namespace std;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)

int dp[300][300], W[300];

int main() {
  while (true) {
    int N; scanf("%d", &N);
    if (not N) break;
    REP(i, N) scanf("%d", W + i);
    memset(dp, 0, sizeof(dp));
    FOR(w, 2, N + 1) REP(left, N - w + 1) {
      // [left, right]
      int right = left + w - 1;
      if (dp[left + 1][right - 1] == w - 2 and abs(W[left] - W[right]) <= 1) {
        dp[left][right] = w;
      } else {
        FOR(mid, left, right) {
          dp[left][right] = max(dp[left][right], dp[left][mid] + dp[mid + 1][right]);
        }
      }
    }
    printf("%d\n", dp[0][N - 1]);
  }

  return 0;
}

C++: #include<bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int> >
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vectors<decltype(inner)>(x, inner); }
using ll = long long;
using ld =long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int W,H;
bool inrange(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
using namespace std;
typedef pair<int, int> pii;
typedef pair<int,pii> piii;
#define MP make_pair
signed main(){
    int n;
    while(cin >> n,n){
        vector<vector<int>>dp(n,vector<int>(n,0));
        vector<int>array(n);
        rep(i,n) cin >> array[i];
        rep(k,n)rep(i,n-1){
            if(!k || i+k>n-1)continue;
            if(k == 1){
                if(abs(array[i]-array[i+1]) <= 1) dp[i][i+k] = 1;
            } else{
                if((abs(array[i]-array[i+k])<=1 && dp[i+1][i+k-1])){
                    cmax(dp[i][i+k],dp[i+1][i+k-1]+2);
                }
                for(int j = i+1; j<i+k;j++){
                    if(dp[i][j] && dp[j+1][i+k])cmax(dp[i][i+k],dp[i][j] + dp[j+1][i+k]);
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++){
            for(int j = n-1; j>=0;--j){
                if(dp[i][j]){
                    ans += j-i+1;
                    i = j;
                    break;
                }
            }
        }
        cout << ans << endl;
    }
}