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 <vector>
#include <cstdlib>
int main(){
    while(1){
        int n;
        std::cin >> n;
        if(n == 0) break;
        std::vector<int> w(n);
        for(int i = 0; i < n; i++)std::cin >> w[i];
        int dp[n + 1][n + 1];
        for(int i = 0; i < n + 1; i++)for(int j = 0; j < n + 1; j++)dp[i][j] = 0;
        for(int h = 2; h < n + 1; h+= 1){
            for(int l = 0; l < n; l++){
                int r = l + h;
                if(r > n) break;
                if(dp[l+1][r -1] == h - 2 && std::abs(w[l] - w[r - 1]) <= 1) dp[l][r] = h;
                for(int mid = l + 1; mid < r; mid++){
                    dp[l][r] = std::max(dp[l][r], dp[l][mid] + dp[mid][r]);
                }
            }
        }
        std::cout << dp[0][n] << std::endl;
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define FOR(i, a, n) for(int i=a; i<n; i++)
#define RFOR(i, a, n) for(int i=n-1; i>=a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 0, n)

int main()
{
	int n;
	while (cin >> n, n) {
		int dp[301][301] = {};
		int w[301];
		REP(i, n) cin >> w[i];

		FOR(k, 1, n) {
			for (int i = 0; i + k < n; i++) {
				if (abs(w[i] - w[i + k]) <= 1) {
					if (dp[i + 1][i + k - 1] == k - 1) {
						dp[i][i + k] = dp[i + 1][i + k - 1] + 2;
					}
				}
				for (int j = i; j < i + k; j++) {
					dp[i][i + k] = max(dp[i][i + k], dp[i][j] + dp[j + 1][i + k]);
				}
			}
		}

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

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

int dp[310][310];
int w[310];

int fall(int left, int right) {
    if (dp[left][right] >= 0) return dp[left][right];
    if (left==right) return dp[left][right] = 0;
    if (left + 1 == right) {
        if (abs(w[left] - w[right]) <= 1) return dp[left][right] = 2;
        else return dp[left][right] = 0;
    }

    for (int mid=left; mid<=right-1; mid++) {
        dp[left][right] = max(dp[left][right], fall(left, mid) + fall(mid+1, right));
    }

    if (right - left >= 3 && dp[left+1][right-1] == right - left - 1) {
        if (abs(w[left] - w[right]) <= 1) dp[left][right] = right - left + 1;
    }
    return dp[left][right];
}

void daruma (int N) {
    rep(i, N) cin >> w[i];
    rep(i, N) rep(j, N) dp[i][j] = -1;

    int ans = fall(0, N-1);
    cout << ans << endl;

}

int main() {
    int n;
    while (cin >> n, n != 0) {
        daruma(n);
    }

    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(left == right)return dp[left][right] = 0;
	if(dp[left][right] != -1)return dp[left][right];
	if((right-left+1)%2 == 1){
		return dp[left][right] = max(recursive(left,right-1),recursive(left+1,right));
	}
	if(table[left][(right-left+1)/2] == true)return dp[left][right] = right-left+1;
	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];
	dp = new int*[301];
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		dp[i] = new int[301];
	}

	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 <= 300; k++){
				dp[i][k] = -1;
			}
		}

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

		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;

				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 <iostream>
#include <fstream>
#include <cassert>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#include <time.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<28;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

int main(){
	while(1){
		int n;
		cin>>n;
		if(!n) break;
		vi a(n);
		for(int i=0;i<n;i++) cin>>a[i];
		vvi dp(n,vi(n+1));
		for(int j=1;j<=n;j++) for(int i=0;i<j;i++){
			dp[i][j]=dp[i][j-1];
			for(int k=j-2;k>=i;k--) if(abs(a[j-1]-a[k])<2&&dp[k+1][j-1]==j-k-2) dp[i][j]=max(dp[i][j],dp[i][k]+j-k);
		}
		cout<<dp[0][n]<<endl;
	}
}