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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

#define MAX 300
int n;
int w[MAX];
map<string, int> wmap;

string nToStr(int n) {
	string tmp = "";
	for (int i = 0; i < n; i++) {
		tmp += "1";
	}
	return tmp;
}

int next(string s, int now) {
	for (int i = now+1; i < n; i++) {
		if (s[i] == '1') return i;
	}
	return n;
}

string pop(string s,int a,int b) { //w???a????????¨b?????????????????????
	string t =s;
	t[a] = '0';
	t[b] = '0';
	return t;
}

int daruma(string s) {
	if (wmap.find(s) != wmap.end())
	{
		 return wmap[s];
	}

	int max = 0;
	for (int i = 0; i < n - 1; i++) {
		if (s[i] == '1') {
			if (next(s, i) != n) {
				if (abs(w[i] - w[next(s, i)]) <= 1) {
					int tmp = daruma(pop(s, i, next(s, i))) + 2;
					if (tmp > max) max = tmp;
				}
			}
		}
	}

	wmap.insert(map<string, int>::value_type(s, max));
	return max;
}

int main() {
	cin >> n;
	while (n) {
		wmap.clear();
		for (int i = 0; i < n; i++) {
			cin >> w[i];
		}

		cout << daruma(nToStr(n)) << endl;

		cin >> n;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

auto dp = make_v<int>(1, 1);
vector<int> w;

int dfs(int l, int r) {
  if (~dp[l][r]) return dp[l][r];
  if (r - l <= 1) return 0;

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

  return res;
}

int main() {
  int n;
  while (cin >> n && n) {
    w = vector<int>(n);
    REP(i, n) cin >> w[i];

    dp = make_v<int>(n+1, n+1);
    fill_v<int>(dp, -1);

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

C++: #include <bits/stdc++.h>
using namespace std;

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

int main(){
    int n;
    while(cin >> n, n){
        for(int i = 0; i < n; i++) cin >> w[i];
        memset(dp, 0, sizeof dp);

        for(int len = 2; len <= n; len++){
            for(int l = 0; l + len <= n; l++){
                for(int c = l + 1; c < l + len; c++){
                    dp[l][l + len] = max(dp[l][l + len], dp[l][c] + dp[c][l + len]);
                }
                if(abs(w[l] - w[l + len - 1]) <= 1 && dp[l + 1][l + len - 1] == len - 2){
                    dp[l][l + len] = len;
                }
                //printf("[%d, %d) = %d\n", l, l + len, dp[l][len] );
            }
        }

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

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

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

int rec(int l,int r){
    if(dp[l][r]!=-1) return dp[l][r];
    if(abs(l-r)<=1) return 0;

    int res=0;
    if(abs(w[l]-w[r-1])<=1 && rec(l+1,r-1)==r-l-2){
        res=r-l;
    }
    for(int mid=l+1;mid<=r-1;mid++){
        res=max(res,rec(l,mid)+rec(mid,r));
    }
    return dp[l][r]=res;
}

int main() {
    while(1){
        int n;
        cin>>n;
        if(n==0) break;
        rep(i,n) cin>>w[i];
        rep(i,310) rep(j,310) dp[i][j]=-1;
        cout<<rec(0,n)<<endl;
    }
    return 0;
}