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<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int i = 0;i < (n);i++)cerr << v[i] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];
int n;
vector<int> node;

int dfs(int l,int r)
{
	if(l > r)return 0;
	if(l == r)return 0;
	if(dp[l][r] != -1)return dp[l][r];
	if(r - l == 1)return (abs(node[l]-node[r])<2)?2:0;
	
	int ret = 0;
	
	if(dfs(l+1,r-1)==r-l-1&&(abs(node[l]-node[r])<2))
	{
		return r-l+1;
	}
	
	for(int i=1;i < r-l;i++)
	{
		ret = max(ret,dfs(l,l+i)+dfs(l+i+1,r));
	}
	
	return ret;
}

int main()
{
	while(cin >> n,n)
	{
		
		REP(i,310)REP(j,310)dp[i][j] = -1;
		node.clear();
		
		REP(i,n)
		{
			int tmp;cin >> tmp;
			node.PB(tmp);
		}
		
		cout << dfs(0,n-1) << endl;
		
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; }
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 2; } }
		for (int h = 0; h < 25; h++) {
			for (int i = 2; i <= n; i++) {
				for (int j = 0; j < n; j++) {
					int a1 = j, a2 = i + j - 1; if (a2 >= n)continue;
					if (dp[a1][a2] == 0)continue;
					if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)dp[a1 - 1][a2 + 1] = i + 2;
				}
			}
			for (int i = 1; i <= n; i++) {
				for (int j = 0; j <= n; j++) {
					int a1 = j, a3 = i + j; if (a3 >= n)continue;
					for (int k = a1 + 1; k <= a3; k++) {
						int a2 = k;
						dp[a1][a3] = max(dp[a1][a3], dp[a1][a2 - 1] + dp[a2][a3]);
					}
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<(n);i++)
#define loop(i,a,n) for(i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main(void) {
    int i,j;
    int n;
    while(cin >> n, n){
        vi a(n);
        rep(i,n) cin >> a[i];
        if(n%2){
            a.push_back(INF);
            n++;
        }

        int dp[300][300]={};

        for(int k = 1; k < n; k += 2){
            rep(i, n-k+1){
                if(dp[i+1][i+k-1] == k-1){//間が全部消える
                    dp[i][i+k] = dp[i+1][i+k-1] + (abs(a[i] - a[i+k]) <= 1) * 2;
                }
                for(j = i+1; j <= i+k-1; j += 2){
                    dp[i][i+k] = max(dp[i][i+k], dp[i][j]+dp[j+1][i+k]);
                }
            }
        }

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

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0; i<n; ++i)
#define OK(x,y) abs(w[x]-w[y])<=1
int n, w[311];
int dp[311][311];

int main(){
  while(cin>>n&&n){
    REP(i,n) cin>>w[i];
    memset(dp, 0, sizeof dp);

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

        for(int k=i+1; k<=i+j-1; k++){
          dp[i][i+j]=max(dp[i][i+j], dp[i][k]+dp[k+1][i+j]);
        }
      }
    }
    cout << dp[0][n-1] << endl;
  }
  return 0;
}

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