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;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
//const ull mod = 1e9 + 7;
const ll mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
  os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

// conversion number to bit
string num2bit(ll num, ll len){
  string bit = "";
  REP(i, len){
    bit += char('0'+(num>>i & 1));
  }
  return bit;
}

ll dp[303][303];

ll rec(ll l, ll r, vector<ll> &w){
	if(dp[l][r]>=0) return dp[l][r];
	if(l>=r){
		dp[l][r] = 0;
		return 0;
	}
	if(l+1==r){
		dp[l][r] = 1;
		return 1;
	}

	ll res = rec(l+1, r-1, w);
	if(res != 0 || abs(w[l]-w[r-1])>1){
		res += 2;
	}

	for(int m=l+1;m<r;m++){
		ll tmp1 = rec(l, m, w);
		ll tmp2 = rec(m, r, w);
		chmin(res, tmp1+tmp2);
	}

	dp[l][r] = res;
	return res;
}

void solve(){
	ll n;
	cin >> n;
	if(n==0) exit(0);
	vector<ll> w(n);
	REP(i, n) cin >> w[i];

	REP(i, 303)REP(j, 303) dp[i][j] = -1;

	ll tmp = rec(0, n, w);

	/*
	REP(i, n+1){
		REP(j, n+1){
			cerr << dp[i][j] << " ";
		}
		cerr << endl;
	}
	*/

	cout << n-tmp << endl;
}

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

    while(1){
    	solve();
    }
    return 0;
}

C++: #include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int n;
V<int> w;
V< V<int> > dp;

int rec(ll l, ll r) {
    if (l == r) return 0;
    if (l + 1 == r) return (abs(w[l] - w[r]) <= 1 ? 2 : 0);
    if (dp[l][r] != -1) return dp[l][r];
    else {
        if (abs(w[l] - w[r]) <= 1 && rec(l + 1, r - 1) == r - l - 1) return dp[l][r] = r - l - 1 + 2;
        else {
            int ret = 0;
            for (int m = l; m < r; m++) chmax(ret, rec(l, m) + rec(m + 1, r));
            return dp[l][r] = ret;
        }
    }
    return 0;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        cin >> n;
        if (n == 0) break;
        w.resize(n);
        for (int i = 0; i < n; i++) cin >> w[i];
        dp.resize(n, V<int>(n));
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = -1;
        cout << rec(0, n - 1) << '\n';
    }
    
    return 0;
}

C++: #include<bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vector<int> >
using ll = long long;
using ld =long double;
#define int ll
#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<bool>>dp(n,vector<bool>(n,0));
		vector<int>array(n);
		rep(i,n) cin >> array[i];
		rep(k,n)rep(i,n){
			if(!k || i+k>n-1)continue;
			if(k == 1){
				if(abs(array[i]-array[i+1]) <= 1) dp[i][i+k] = true;
			} else{
				if((abs(array[i]-array[i+k])<=1 && dp[i+1][i+k-1])){
					dp[i][i + k] = true;
				}
				for(int j = i+1; j<i+k;j++){
					dp[i][i + k] = dp[i][i + k] or (dp[i][j] and dp[j + 1][i + k]);
				}
			}
		}
		int ans = 0;
		for (int s = 0;s < n;s++){
			int temp = 0;
			for(int i = s; i<n;i++){
				for(int j = n-1; j >= 0;--j){
					if(dp[i][j]){
						temp += j-i+1;
						i = j;
					}
				}
			}
			cmax(ans, temp);
		}
		cout << ans << endl;
	}
}