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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

int N,maximum;

void recursive(int table[],int num,int SIZE){

	maximum = max(maximum,2*num);

	int max_chain = 0,tmp_chain,nextTable[SIZE],next_index = 0;

	for(int i = 1; i <= SIZE-1; i++){
		if(abs(table[i] - table[i-1]) <= 1){
			if(i == SIZE-1){
				if(max_chain == 0){
					nextTable[next_index++] = SIZE-1;
				}
			}else{
				tmp_chain = 0;
				for(int k = 1;(i-1)-k>=0 && i+k <= SIZE-1;k++){
					if(abs(table[(i-1)-k] - table[i+k]) <= 1){
						tmp_chain++;
					}
					else{
						break;
					}
				}
				if(tmp_chain > max_chain){
					max_chain = tmp_chain;
					next_index = 0;
					nextTable[next_index++] = i;
				}else if(tmp_chain == max_chain){
					nextTable[next_index++] = i;
				}
			}
		}
	}

	if(next_index == 0)return;

	for(int i = 0; i < next_index;i++){
		int new_table[SIZE-2],next_size = 0;
		for(int k = 0; k < SIZE; k++){
			if(k != nextTable[i] && k != nextTable[i]-1){
				new_table[next_size++] = table[k];
			}
		}
		recursive(new_table,num+1,next_size);
	}
}

void func(){

	int first[N];
	for(int i = 0; i < N; i++)scanf("%d",&first[i]);

	if(N == 1){
		printf("0\n");
		return;
	}else if(N == 2){
		if(abs(first[0] - first[1] <= 1)){
			printf("1\n");
		}else{
			printf("0\n");
		}
		return;
	}

	recursive(first,0,N);

	printf("%d\n",maximum);
}


int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;
		maximum = 0;

		func();
	}

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    int w[n],i,j,k=0;
    for(i=0;i<n;i++) cin>>w[i];
    bool p[n][n];
    memset(p,0,sizeof(p));
    for(j=0;j+1<n;j++) if(abs(w[j+1]-w[j])<2) p[j][j+1]=1;
    for(i=3;i<n;i+=2)
      for(j=0;j+i<n;j++)
	if((p[j+1][j+i-1]&&abs(w[j+i]-w[j])<2)||
	   (p[j][j+i-2]&&abs(w[j+i]-w[j+i-1])<2)||
	   (p[j+2][j+i]&&abs(w[j+1]-w[j])<2)) p[j][j+i]=1;
    int dp[n+1];
    memset(dp,0,sizeof(dp));
    for(i=0;i<n;i++)
      for(j=0;i+j<=n;j++)
	dp[i+j+1]=max(dp[i+j+1],dp[i]+p[i][i+j]*(j+1));
    cout<<dp[n]<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INFTY 200000000000
long long dp[310][310];
vector<long long> a(310);
int b;
long long ch(int l,int r){
    if(dp[l][r]>=0) return dp[l][r];
    if(l==r){
        return dp[l][r]=0;
    }
    for (int k = l; k < r; k++)
    {
        if(abs(a[k]-a[r])<=1 && ch(k+1,r-1) == r-k-1 &&ch(l,k-1)==ch(l,r-1)+1+k-r){
            return dp[l][r] = max(dp[l][r],ch(l,r-1)+2);
        }
    }
    for (int k = l+1; k < r+1; k++)
    {
        if(abs(a[k]-a[l])<=1 && ch(l+1,k-1) == k-l-1 && ch(k+1,r)==ch(l+1,r)+1+l-k){
            return dp[l][r] = max(dp[l][r],ch(l+1,r)+2);
        }
    }
    return dp[l][r] = max(ch(l+1,r),ch(l,r-1));
}
int main(){
    while(1){
    cin >> b;
    if(b==0) return 0;
    for (int i = 0; i < b; i++)
    {
        cin >> a[i];
    }
    
    for (int i = 0; i < 305;i++)
    {
        for(int j=0;j<305;j++){
            dp[i][j] = -1;
        }
    }
    long long ans=0;
   
   ans = ch(0,b-1);
    
    cout << ans << endl;
    }
    
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

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

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	
	while(1) {
		cin1(n);
		if (n == 0) break;

		vvi dp(n,vi(n,0)); //dp[i][j] i??????j??????????????§?????°
		vi daruma(n,0);
		rep(i,0,n) {
			cin1(daruma[i]);
		}

		rep(i,1,n) {
			if (abs(daruma[i-1] - daruma[i]) <= 1) {
				dp[i-1][i] = 2;
			}
		}

		for (int i = 2; i <= n; i++) { //??????
			for (int l = 0; l < n - i + 1; l++) { //??????
				int r = l + i - 1;
				if (dp[l+1][r-1] == i - 2 and abs(daruma[l]-daruma[r]) <= 1) dp[l][r] = i; 
				for (int k = l; k < r; k++) {
					dp[l][r] = max(dp[l][r],dp[l][k] + dp[k+1][r]);	
				}
				
			}
		}

		cout(dp[0][n-1]);
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define REP(i, n) FOR(i, 0, n)
#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)>(b)?(b):(a))
#define abs(a) max((a), -(a))

int n;
int w[304];

int dp[303][303];

int main() {

    while(cin>>n,n) {
        REP(i, n)cin>>w[i];

        REP(i, n) {
            dp[i][i] = 1;
            if(i<n-1) {
                dp[i][i+1] = abs(w[i]-w[i+1])<=1?0:2;
            }
        }

        FOR(i, 2, n) { // 幅
            FOR(j, 0, n-i) {
                dp[j][j+i] = i+1;

                if(dp[j+1][j+i-1]==0 && abs(w[j]-w[j+i])<=1) {
                    dp[j][j+i] = 0;
                    continue;
                }

                REP(k, i) {
                    int a = dp[j][j+k]+dp[j+k+1][j+i];
                    dp[j][j+i] = min(a, dp[j][j+i]);
                    if(a==0)break;
                }

            }
        }

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

    return 0;
}