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++: // #Sazaの1日1AC
#include <bits/stdc++.h>
using namespace std;
const long long mod = 10000;
const long long INF = 9e18;
const double PI = 3.14159265358979323846;

//マクロ
//型エイリアス
using ll = long long;
using P = pair<long long, long long>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vP = vector<pair<long long, long long>>;
//ショートカット
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, k, n) for (int i = k; i < n; i++) //半開区間
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define mkp make_pair
//入力
#define vin(v, N) for (long long i = 0; i < N; i++) cin >> v.at(i)
#define lin(n)long long n;cin >> n
#define chin(x)char x;cin >> x;
#define sin(s)string s;cin >> s;
#define vlin(v, N)vector<long long> v(N);for (long long i = 0; i < N; i++)cin >> v.at(i)
#define max(a, b) max((ll)a, (ll)b)
#define min(a, b) min((ll)a, (ll)b)

//関数
//最大公約数
long long gcd(long long m, long long n){
  long long a = max(m, n);
  long long b = min(m, n);
  long long r = a % b;
  while (r != 0){
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
// 最小公倍数
long long lcd(long long m, long long n){
  return m * n / gcd(m, n);
}
//xのn乗
long long power(long long x, long long N){
  long long ret = 1;
  for (long long i = 0; i < N; i++)
    ret *= x;
  return ret;
}
//繰り返し二乗法
long long repeat_squaring(long long x, long long n){
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
  {
    long long t = repeat_squaring(x, n / 2);
    return t * t % mod;
  }
  else
    return x * repeat_squaring(x, n - 1);
}
//素因数分解(mapで返す)
map<long long, long long> factor(long long p){
  ll p1 = p;
  map<long long, long long> ret;
  for (long long i = 2; i * i <= p1; i++)
  {
    while (p % i == 0)
    {
      ret[i]++;
      p /= i;
    }
  }
  if (p != 1)
    ret[p]++;
  return ret;
}
//素数判定
bool is_prime(long long N){
  for (long long i = 2; i * i <= N; i++)
  {
    if (N % i == 0)
      return false;
  }
  return true;
}
//最大値更新
void chmax(long long &a, long long b){
  a = max(a, b);
}
//最小値更新
void chmin(long long &a, long long b){
  a = min(a, b);
}

//構造体
//Union-Find木
struct UnionFind
{
  //メンバ変数
  vector<long long> par; //par[i]:=頂点iの親
  vector<long long> s;   //s[i]:=頂点iが属する集合の個数(iが根のとき)

  //コンストラクタ
  UnionFind(long long N) : par(N), s(N){
    for (long long i = 0; i < N; i++)
    {
      par[i] = i; //最初はすべてが根として初期化
      s[i] = 1;
    }
  }
  //メンバ関数
  //root(i):=頂点iの根
  long long root(long long i){
    if (par[i] == i)
      return i;
    par[i] = root(par[i]); //経路圧縮
    return root(par[i]);   //再帰
  }
  //size[i]:=iが属する集合の個数
  long long size(long long i){
    return s[root(i)];
  }
  //same(x,y) xとyが同じ根を持つか(同じ集合に含まれるか)
  bool same(long long x, long long y){
    return root(x) == root(y);
  }
  //unite(x,y) xの根をyの根に繋げる(集合を合併)
  void unite(long long x, long long y){
    if (!same(x, y)){
      long long rx = root(x);
      long long ry = root(y);
      par[rx] = par[ry];
      s[ry] = s[rx] + s[ry];
    }
  }
};
//Binary Indexed Tree
struct BIT{
  vector<long long> bit;
  long long N;

  BIT(long long n){
    N = n;
    bit.resize(N + 1);
  }

  void add(long long i, long long w){
    for (long long x = i; x <= N; x += x & -x){
      bit[x] += w;
    }
  }
  long long sum(long long i){
    long long ret = 0;
    for (long long x = i; x > 0; x -= x & -x){
      ret += bit[x];
    }
    return ret;
  }
};
/////////////////⊂('ω'⊂ )))Σ≡GO!/////////////////
vvl dp(305,vl(305,-1));
vl a;

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

  ll ret = 0;
  if(abs(a[l] - a[r-1]) <= 1 && rec(l+1, r-1) == r - l -2) ret = r - l;

  for(ll mid = l + 1; mid <= r - 1; mid++) chmax(ret, rec(l, mid) + rec(mid, r));

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



int main(){
  //doubleの桁数
  cout << fixed << setprecision(10);

  while(true){
    lin(N);
    if(N == 0) break;

    a.resize(N);
    rep(i,N) cin >> a[i];
    rep(i,305) rep(j,305) dp[i][j] = -1;

    cout << rec(0,N) << endl;
  }
}

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

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

C++: #include <iostream>
#include <cstring>

constexpr int NUM = 300;
int w[NUM] = { 0 };
bool dp[NUM] = {false}; //[???][???] = ?????¨??????????????????

bool isDropedBetween(int a, int b) {
	if ((b - a) <= 1) return true;
	for (int i = a + 1; i < b; i++) if (!dp[i]) return false;
	return true;
}

bool isDroped(int a, int b) {
	for (int i = a; i <= b; i++) if (!dp[i]) return false;
	return true;
}

int main() {
	int n;
	while (std::cin >> n && n) {
		std::memset(dp, 0, sizeof(bool) * NUM * NUM);
		for (int i = 0; i < n; i++) std::cin >> w[i];

		for (int d = 1; d < n; d += 2) { //?????¨?????????
			for (int i = 0; i < n - d; i++) { //?????¨?????´???
				bool mightDrop = false;
				if (isDropedBetween(i, i + d)) mightDrop = true; //???????????????????????¨????????¨??????
				if (isDroped(i, i + d) || (mightDrop && std::abs(w[i] - w[i + d]) <= 1)) {
					for (int j = 0; j <= d; j++) dp[i + j] = true;
				}
			}
		}
		int count[2] = {0};
		for (int i = 0; i < NUM; i++) {
			if (dp[i]) count[1]++;
			else {
				count[0] += count[1] / 2 * 2;
				count[1] = 0;
			}
		}
		std::cout << count[0] << std::endl;
	}
	return 0;
}