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;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

// 素因数分解; 返り値は(素数, 乗数)のvector配列
vector<pair<ll, int>> factorize(ll n) {
	vector<pair<ll, int>> res;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i) continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1) res.emplace_back(n, 1);
	return res;
}

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };
//constexpr auto MAX = 100000;

// ──────────────────────────────────────────────────────────────────

int n, dp[1010][1010];
vi w;

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 (true) {
		cin >> n;

		if (!n) break;

		w.resize(n);
		rep(i, n) cin >> w[i];

		memset(dp, -1, sizeof(dp));
		cout << rec(0, n) << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int i = 0;i < (n);i++)cerr << v[i] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];
int n;
vector<int> node;

int dfs(int l,int r)
{
	if(l > r)return 0;
	if(l == r)return 0;
	if(dp[l][r] != -1)return dp[l][r];
	if(r - l == 1)return (abs(node[l]-node[r])<2)?2:0;
	
	int ret = 0;
	
	if(dfs(l+1,r-1)==r-l-1&&(abs(node[l]-node[r])<2))
	{
		return r-l+1;
	}
	
	for(int i=0;i < r-l;i++)
	{
		ret = max(ret,dfs(l,l+i)+dfs(l+i+1,r));
	}
	
	return dp[l][r] = ret;
}

int main()
{
	while(cin >> n,n)
	{
		
		REP(i,310)REP(j,310)dp[i][j] = -1;
		node.clear();
		
		REP(i,n)
		{
			int tmp;cin >> tmp;
			node.PB(tmp);
		}
		
		cout << dfs(0,n-1) << endl;
		
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
bool u[305][305],p[305][305];
int dp[305];

int main(){
  int N;
  while(cin>>N,N){
    memset(p,0,sizeof(p));
    memset(u,0,sizeof(u));
    std::vector<int> v(N);
    for(int i=0;i<N;i++)cin>>v[i];
    queue<P> que;
    for(int i=1;i<N;i++)if(abs(v[i-1]-v[i])<2){p[i-1][i]=1;que.push(P(i-1,i));}
    while(que.size()){
      int l=que.front().first;int r=que.front().second;que.pop();
      if(u[l][r])continue;
      u[l][r]=1;
      if(l-1>=0&&r+1<N&&abs(v[l-1]-v[r+1])<2){p[l-1][r+1]=1;que.push(P(l-1,r+1));}
      for(int i=r+2;i<N;i++)if(p[r+1][i]){p[l][i]=1;que.push(P(l,i));}
      for(int i=0;i<l-1;i++)if(p[i][l-1]){p[i][r]=1;que.push(P(i,r));}
    }
    memset(dp,0,sizeof(dp));
    for(int i=0;i<N;i++)dp[i]=p[0][i]*(i+1);
    for(int i=0;i<N;i++)for(int j=1;i+j<N;j++)dp[i+j]=max(dp[i+j],dp[i]+p[i+1][i+j]*j);
    cout<<dp[N-1]<<endl;
  }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int *line;
bool **check;
bool search(int, int);
int main() {
	int n;
	while (cin >> n&&n > 0) {
		line = new int[n];
		check = new bool*[n];
		for (int i = 0; i < n; ++i) {
			cin >> line[i];
			check[i] = new bool[n] {0};
		}
		for (int i = 0, nmax = n - 1; i < nmax; ++i) {
			if (abs(line[i] - line[i + 1]) <= 1) {
				check[i][i + 1] = true;
			}
		}
		for (int wide = 3; wide < n; wide += 2) {
			for (int i = 0, nmax = n - wide; i < nmax; ++i) {
				if (search(i, i + wide)) {
					check[i][i + wide] = true;
				}
			}
		}
		int *dp;
		dp = new int[n + 1]{ 0 };
		for (int i = 0; i < n; ++i) {
			dp[i + 1] = max(dp[i + 1], dp[i]);
			for (int j = i + 1; j < n; j += 2) {
				if (check[i][j]) {
					dp[j + 1] = max(dp[j + 1], dp[i] + j - i + 1);
				}
			}
		}
		cout << dp[n] << endl;
	}
	return 0;
}
bool search(int ps, int pe) {
	if (abs(line[ps] - line[pe]) <= 1 && check[ps + 1][pe - 1])return true;
	for (int i = ps + 1; i < pe; i += 2) {
		if (check[ps][i] && check[i + 1][pe]) {
			return true;
		}
	}
	return false;
}