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>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int a[301],n,dp[301][301];
int 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++){
        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]);
        if(abs(a[j]-a[j+i])<2&&dp[j+1][j+i-1]==i-1)dp[j][j+i]=max(dp[j][j+i],i+1);
      }
    r(i,n)r(j,n)ans=max(ans,dp[i][j]);
    cout<<ans<<endl;
  }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

int memo[300][300];
int dfs(const vector<int> &w, int left, int right){
	if(right - left < 2) return  0;
	if(right - left == 2){
		if(abs(w[left] - w[right - 1]) <= 1) return 2;
		else return 0;
	}
	if(memo[left][right] != -1) return memo[left][right];
	//cout << left << ' ' << right << endl;

	int res = 0;
	range(i,left,right - 1){
		int tmp = 0;
		if(abs(w[i] - w[i + 1]) <= 1){
			//cout << left << ' ' << i << ' ' << i + 2 << ' ' << right << endl;
			if(left < i){
				tmp += dfs(w, left, i);
			}
			if(i + 2 < right){
				tmp += dfs(w, i + 2, right);
			}
			res = max(res, tmp + 2);
		}
	}
	range(i,left,right){
		int tmp = 0;
		if(left < i){
			tmp += dfs(w, left, i);
		}
		if(i + 1 < right){
			tmp += dfs(w, i + 1, right);
		}
		res = max(res, tmp);
	}
	if(dfs(w, left + 1, right - 1) == right - left - 2 && abs(w[left] - w[right - 1]) <= 1){
		//cout << left << ' ' << right << endl;
		res = max(res, right - left);
	}
	return memo[left][right] = res;
}

int main(){
    int n;
    while(cin >> n,n){
        vector<int> w(n);
		memset(memo, -1, sizeof(memo));
        rep(i,n){
            cin >> w[i];
        }

		cout << dfs(w, 0, n) << endl;
    }

}

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int n;
int dp[301][301];
bool take[301][301];
int w[301];

int main() {
    while (cin >> n, n) {
        fill(dp[0], dp[n], 0);
        fill(take[0], take[n], false);
        rep(i, n) cin >> w[i];

        for (int len=0; len<n; ++len) {
            rep(i, n-len) {
                int l = i, r = i+len;

                // [l, k] + [k+1, r] -> [l, r]
                for (int k=l; k<r; ++k) {
                    if (take[l][k] and take[k+1][r]) take[l][r] = true;
                }

                if (len+1 == 2) {
                    if (abs(w[r]-w[l]) <= 1) take[l][r] = true;
                }
                if (take[l][r]) {
                    int ll = l-1, rr = r+1;
                    while (true) {
                        if (ll < 0 or ll >= n or rr < 0 or rr >= n) break;
                        if (abs(w[rr]-w[ll]) >= 2) break;
                        take[ll][rr] = true;
                        ll--; rr++;
                    }
                }
            }
        }

        rep(i, n) for (int j=i+1; j<n; ++j) {
            if (take[i][j]) dp[i][j] = j-i+1;
        }

        rep(i, n) for (int j=i; j<n; ++j) for (int k=i; k<j; ++k) {
            dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);
        }

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

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    while (cin >> n, n) {
        vector<int> ws(n);
        for (auto &&x: ws) cin >> x;

        vector<vector<bool>> memo(n, vector<bool>(n, false));

        // 隣接した２つのブロックが消せるか
        for (int l = 0; l<n-1; l++) {
            int r = l+1;
            if (abs(ws[l]-ws[r]) < 2) 
              memo[l][r] = true;
        }

        // ２つのブロックを指定し、その間の区間が消せるか
        for (int len = 2; len<n; len++) {
            for (int l = 0; l+len<n; l++) {
                int r = l+len;
                for (int c = l+1; c<r; c++)
                    if (memo[l][c-1] && memo[c][r])  
                        memo[l][r] = true;

                if (memo[l+1][r-1] && abs(ws[l]-ws[r]) < 2)  
                    memo[l][r] = true;
            }
        }

        vector<int> dp(n, 0);
        for (int r=1; r<n; r++) {
            dp[r] = dp[r-1];
            for (int l=0; l<n; l++) {
                if (memo[l][r]) {
                    if (l == 0) dp[r] = max(dp[r], r-l+1);
                    else        dp[r] = max(dp[r], dp[l-1] + r-l+1);
                }
            }
        }

        // for (auto x: dp) cout << setw(3) << x; cout << endl;
        cout << *(max_element(dp.begin(), dp.end())) << endl;
    }
}

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

int dp[301][301];
int n;
int a[301];

bool dfs(int l, int r) {
	if (r - l <= 0) return dp[l][r] = true;
	if ((r - l) % 2) return dp[l][r] = false;
	if (dp[l][r] != -1) return dp[l][r];
	
	bool ret = false;
	if (abs(a[l] - a[r - 1]) <= 1) { ret |= dfs(l + 1, r - 1); }
	for (int i = l + 1; i < r; i++) {
		if (ret) { break; }
		ret |= (dfs(l, i) && dfs(i, r));
	}
	return (dp[l][r] = ret);
}

int dp2[301];

int main() {
	int i, j;
	
	while (cin >> n) {
		if (!n) break;
		for (i = 0; i < n; i++) cin >> a[i];
		for (i = 0; i < n; i++) for (j = 0; j <= n; j++) dp[i][j] = -1;
		for (i = 0; i < n; i++) for (j = i; j <= n; j++) dfs(i, j);
		
		for (i = 0; i <= n; i++) dp2[i] = 114514;
		dp2[0] = 0;
		for (i = 0; i < n; i++) {
			dp2[i + 1] = min(dp2[i + 1], dp2[i] + 1);
			for (j = i + 1; j <= n; j++) {
				if (dp[i][j]) {
					dp2[j] = min(dp2[j], dp2[i]);
				}
			}
		}
		cout << n - dp2[n] << endl;
	}
	return 0;
}