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;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n;
int dp[500][500];
vector<int> w;

// [l,r)
int rec(int l, int r){
    if(dp[l][r] >= 0) return dp[l][r];
    if(r-l<2) return 0;

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

int main() {
    while(cin >> n, n){
        w.resize(n);
        REP(i,n){
            cin >> w[i];
        }
        REP(i,n+1) fill(dp[i],dp[i]+n+1,-1);
        cout << rec(0,n) << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <math.h>
#include <set>
  
#define REP(i,n)for (int i=0;i<(n);i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ll long long
using namespace std;
typedef pair<int,int> P;
int N;
int W[301];
int f[301][301];
int main(){
	while(1){
		cin>>N;
		if(N==0)break;
		REP(i,N)cin>>W[i];
		memset(f,false,sizeof(f));
		REP(i,N)f[i][i]=true;
		REP(i,N-1)if(abs(W[i]-W[i+1])<=1)f[i][i+2]=true;
		for(int d=4;d<=N;d+=2){
			REP(i,N-d+1){
				for(int j=i+2;j<=i+d-2;j+=2){
					if(f[i][j]&&f[j][i+d])f[i][i+d]=true;
				}
				if(abs(W[i]-W[i+d-1])<=1&&f[i+1][i+d-1]){
					f[i][i+d]=true;
				}
			}
		}
		int ma[N];
		REP(i,N+1)ma[i]=i;
		REP(i,N){
			for(int j=N;j>=i;j--){
				if(f[i][j]){
					ma[i]=j;
					break;
				}
			}
		}
		/*REP(i,N)cout<<ma[i]<<" ";cout<<endl;
		REP(i,N+1){REP(j,N+1)cout<<f[i][j]<<" ";cout<<endl;}*/
		int ans=0;
		REP(i,N){
			int p=i,cnt=0;
			while(p<=N){
				int pp=ma[p];
				cnt+=(pp-p);
				p=pp+1;
			}
			ans=max(ans,cnt);
		}
		cout<<ans<<endl;
	}
}

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

#define rep(i, n) REP(i, 0, n)
#define repr(i, n) for (int i = (n); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int dp[301][301];
int w[300];

int daruma(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 && daruma(l + 1, r - 1) == r - l - 2)
        res = r - l;
    for (int i = l + 1; i <= r - 1; i++)
        res = max(res, daruma(l, i) + daruma(i, r));

    return dp[l][r] = res;
}

int main()
{
    int n;
    while (cin >> n,n)
    {
        rep(l, 301) rep(r, 301) dp[l][r] = -1;
        rep(i, n) cin >> w[i];
        OP(daruma(0, n));
    }
    return 0;
}

C++: #include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge> > pqlE;

int main() {
	while (true) {
		int N;
		cin >> N;
		if (N == 0)break;
		vi a(N);
		rep(i, N)cin >> a[i];
		vvi dp(N, vi(N + 1, 0));
		repl(w, 1, N + 1) {
			rep(i, N) {
				int j = i + w;
				if (j > N)continue;
				if (w >= 2 && dp[i + 1][j - 1] == (w - 2) && abs(a[i] - a[j - 1]) <= 1) {
					dp[i][j] = w;
				}
				else {
					repl(k, i + 1, j) {
						dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
					}
				}
			}
		}
		/*
		rep(i, N) {
			repl(j, 1, N + 1) {
				cout << dp[i][j];
			}
			cout << endl;
		}
		*/
		cout << dp[0][N] << endl;
	}

	return 0;
}