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 long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int n;
int w[302];
bool can[302][302];
int main() {
    //cout.precision(10);
    while(true) {
        cin >> n;
        if(n == 0) break;
        for(int i = 1; i <= n; i++) {
            cin >> w[i];
        }
        for(int i = 0; i <= n + 1; i++) {
            for(int j = 0; j <= n + 1; j++) {
                can[i][j] = false;
            }
            can[i][i] = true;
        }
        for(int delta = 2; delta <= n; delta++) {
            for(int from = 1; from + delta <= n + 1; from++) {
                int to = from + delta;
                for(int mid = from; mid <= to; mid++) {
                    if(can[from][mid] && can[mid][to]) can[from][to] = true;
                }
                if(can[from + 1][to - 1]) {
                    if(abs(w[from] - w[to - 1]) <= 1) can[from][to] = true;
                }
                //if(can[from][to]) cout << from << " " << to << endl;
            }
        }
        int dp[305];
        for(int i = 0; i <= 304; i++) {
            dp[i] = 0;
        }
        for(int now = 1; now <= n; now++) {
            dp[now] = dp[now - 1];
            for(int from = 1; from <= now; from++) {
                if(can[from][now + 1]) {
                    dp[now] = max(dp[now], dp[from - 1] + (now + 1 - from));
                }
            }
            //cout << now << " " << dp[now] << endl;
        }
        cout << max(dp[n], dp[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];
        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 N 301
using namespace std;
int w[N],mem[N][N],used[N][N];

int dfs(int l,int r){
  if(l==r) return 0;
  if(r-l==1) return 2*(abs(w[l]-w[r])<=1);
  if(used[l][r]++) return mem[l][r];
  
  int res=0;
  if(abs(w[l]-w[r])<=1&&dfs(l+1,r-1)==r-l-1)res=2+dfs(l+1,r-1);
  
  for(int i=l;i<r;i++) res=max(res,dfs(l,i)+dfs(i+1,r));
  return mem[l][r]=res;
}

int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>w[i];
    memset(used,0,sizeof(used));
    cout<<dfs(0,n-1)<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

bool** table;
int** dp;
int N;

int recursive(int left,int right){

	if(dp[left][right] != -1)return dp[left][right]; //?????¢???????????´???
	if(left == right)return 0;
	if((right-left+1)%2 == 1){ //??????????\???°?????´???
		return max(recursive(left,right-1),recursive(left+1,right));
	}
	if(table[left][(right-left+1)/2] == true)return dp[left][right] = right-left+1; //left???right?????¨????¶?????????´???
	if(right == left+1 && table[left][1] == false)return 0;

	int ans = 0;
	for(int i = left+1; i <= right-2; i++){
		ans = max(ans,recursive(left,i)+recursive(i+1,right));
	}
	return dp[left][right] = ans;
}

int main(){

	int data[301];
	table = new bool*[301]; //table[left][right] = left???????§?????????????2*right??????????????¨?¶??????§??????????????????
	dp = new int*[301]; //?????????????????????????????§?¶????????????°
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		dp[i] = new int[151];
	}

	bool FLG;

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

		if(N <= 1){
			printf("0\n");
			continue;
		}

		//?¶??????????????????????????????????
		for(int i = 0; i <= 300; i++){
			for(int k = 0; k <= 150; k++)table[i][k] = false;
			for(int k = 0; k <= 150; k++){
				dp[i][k] = -1;
			}
		}

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

		//????????????2???????¨?
		for(int i = 1; i <= N-1; i++){
			if(abs(data[i+1]-data[i]) <= 1)table[i][1] = true;

		}

		//?¶??????????????????????????¨????
		for(int length = 4; length <= N; length += 2){ //??????????????????
			for(int left = 1; left+length -1 <= N; left++){ //??????????????????
				FLG = false;

				if(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)FLG = true;//??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???

				if(!FLG){
					for(int i = 2; i <= length-2; i += 2){
						if(table[left][i/2] == true && table[left+i][(length-i)/2] == true){
							FLG = true;
							break;
						}
					}
				}
				if(FLG){
					table[left][length/2] = true;
				}
			}
		}

		printf("%d\n",recursive(1,N));
	}

	return 0;
}

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

int n, a[1010];
int dp[1010][1010], dp2[1010] = {};

int ok(int l, int r){
    int &res = dp[l][r];
    if(res != -1) return res;
    res = false;
    if(l == r) res = true;
    for(int i = l+2; i < r; i += 2) if(ok(l,i) && ok(i,r)) res = true;
    if(abs(a[l] - a[r-1]) <= 1 && ok(l+1, r-1)) res = true;
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(cin >> n && n){
        rep(i, n) cin >> a[i];
        memset(dp, -1, sizeof dp);
        memset(dp2, 0, sizeof dp2);
        rep(i, n){
            dp2[i+1] = max(dp2[i+1], dp2[i]);
            for(int j = i + 2; j <= n; j += 2){
                if(ok(i, j)) dp2[j] = max(dp2[j], dp2[i] + j - i);
            }
        }
        cout << dp2[n] << endl;
    }
}