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 <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;
            }
        }
        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;

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}

int N;
vector<int> w;

int solve() {
    vector<vector<int>> dp(N+1, vector<int>(N+1, -N));
    for (int i = 0; i <= N; ++i) {
        dp[i][i] = 0;
        if (i < N) dp[i][i+1] = 0;
    }
    for (int bet = 2; bet <= N; ++bet) {
        for (int i = 0; i + bet <= N; ++i) {
            int j = i + bet;
 
            // 1. 全部取り除けるかもしれないとき
            if ( abs(w[i]- w[j-1]) <= 1){
                if ( dp[i+1][j-1] == j-i-2) chmax(dp[i][j],j-i);
            }
            
            // 2. otherwise
            for (int k = i+1; k <= j-1; ++k) {
                chmax(dp[i][j], dp[i][k] + dp[k][j]);
            }
            
        }
    }
    return dp[0][N];
}

int main() {
    while (cin >> N, N) {
        w.resize(N);
        for (int i = 0; i < N; ++i) cin >> w[i];
        cout << solve() << endl;
    }
}

C++: 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstring>
 
using namespace std;
 
#define rep(i,n) for(int i = 0; i<n ; i++)
#define FOR(i,a,n) for(int i=a; i<n; i++)
 
#define RANGE(a,i,b) ((a)<=(i)&&(i)<=(b))
 
#define pb push_back
#define mp make_pair
 
#define ALL(a) (a).begin(), (a).end()
 
#define SET(a,c) memset(a,c,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
 
#define INF 1001001001
#define LLINF 1001001001001001001LL
 
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
 
#define PQ priority_queue
 
#define FST first
#define SEC second
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
 


int dp[300][300];

int w[300];
//------------------HEAD---------------------
int main(){
    while(1){
        int n;cin>>n;
        if(n==0)break;
        rep(i,n){
            cin>>w[i];
        }//cerr<<w[200]<<" "<<w[201]<<endl;
        rep(i,n)rep(j,n){/*cerr<<w[200]<<" "<<w[201]<<endl;*/dp[i][j]=0;}
        //cerr<<dp[0][n-1]<<endl;
        //cerr<<w[200]<<" "<<w[201]<<endl;
        FOR(i,1,n){
            rep(j,n-i){
                FOR(k,j,j+i){
                    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(w[j]-w[j+i])<2){
                    //cerr<<j<<" "<<j+i<<" "<<dp[j+1][j+i-1]<< " "<<w[j]<<" "<<w[j+i]<<" "<<w[j]-w[j+1]<<endl;
                    dp[j][j+i]=i+1;
                }
            }
        }
        /*rep(i,n){
            rep(j,n){
                cout<<dp[i][j]<<" ";
            }cout<<endl;
        }*/
        cout<<dp[0][n-1]<<endl;

    }

    

    return 0;
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int a[301],n,dp[301][301];
main(){
  while(cin>>n,n){
    int ans=0;
    r(i,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<n;i+=2)
      for(int j=0;j<n-i;j++){
        dp[j][j+i]=dp[j+1][j+i-1];
        if(abs(a[j]-a[j+i])<2&&dp[j+1][j+i-1]==i-1)dp[j][j+i]=i+1;
        else for(int k=1;k<i;k+=2)dp[j][j+i]=max(dp[j][j+i],dp[j][j+k]+dp[j+k+1][j+i]);
      }
    r(i,n)r(j,n)ans=max(ans,dp[i][j]);
    cout<<ans<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define MAX_N 40
#define INF 1000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int w[300];
bool dp[300][300];
int ddp[301];

void solve(){
    memset(dp,0,sizeof(dp));
    rep(i,n) cin >> w[i];
    for(int i = 1; i < n; i+= 2){
        for(int j = 0; j < n-i; j++){
            if(abs(w[j]-w[j+i]) <= 1){
                if(i == 1 || dp[j+1][j+i-1] == true){
                    dp[j][j+i] = true;
                }
            }
            for(int k = j+1; k <= j+i-2; k += 2){
                if(dp[j][k] == true && dp[k+1][j+i] == true){
                    dp[j][j+i] = true;
                }
            }
        }
    }
    rep(i,n) ddp[i] = 0;
    for(int i = 1; i < n; i += 2) if(dp[0][i]) ddp[i] = i+1;
    for(int i = 1; i < n; i++){
        ddp[i] = max(ddp[i],ddp[i-1]);
        for(int j = 1; j < n-i; j += 2){
            if(dp[i][i+j]) ddp[i+j] = max(ddp[i+j],ddp[i-1]+j+1);
        }
    }
    cout << ddp[n-1] << endl;
}

int main(){
    while(cin >> n){
        if(n == 0) break;
        solve();
    }
}