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++: #define _GLIBCXX_DEBUG

#include <bits/stdc++.h>


using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = { false };
vector<int> w(n);

int rec(int l, int r) {
  if (r - l == 0) {
    return memo[l][r] = 2;
  }
  if (r - l == 1) {
    return memo[l][r] = 1;
  }
  
  if (r - l  - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return memo[l][r] = 2;
    }
  }
  if (memo[l][r]) {
    return memo[l][r];
  }

  for (int t = l + 1; t < r; t++) {
    int temp  = rec(l, t);
    int temp2 = rec(t, r);
    int temp3 = rec(l + 1, t);
    int temp4 = rec(t, r - 1);
    
    if (temp == 2 && temp2 == 2) {
      return memo[l][r] = 2;
    }
    if (abs(w[l] - w[r - 1]) <= 1 && temp3 == 2 && temp4 == 2) {
      return memo[l][r] = 2;
    }
  }

  return memo[l][r] = 1;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }

    w.clear();
    w.resize(n);

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

    cerr << " " << rec(0, n) << endl;

    int dp[310] = { 0 };
    dp[0] = 0;

    for (int r = 0; r <= n; r++) {
      dp[r] = dp[r - 1];
      for (int l = 0; l < r; l++) {
        if (memo[l][r] == 2) {
          dp[r] = max(dp[r], dp[l] + (r - l));
        }
      }
    }
    

    cout << dp[n] << endl;

  }

  

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll n;
ll dp[301][301];

int main(){
	while(cin >> n && n){
		vector<ll> v(n);
		rep(i,n)cin >> v[i];
		rep(i,301)rep(j,301)dp[i][j] = 0;
		for(ll b = 2;b <= n;b++){
			for(ll i = 0;i + b <= n;i++){
				if(abs(v[i] - v[i+b-1]) <= 1 && (dp[i+1][i+b-2] == b-2 || b == 2)){
					dp[i][i+b-1] = max(dp[i][i+b-1],dp[i+1][i+b-2] + 2);
				}
				for(ll j = i+1;j < i+b-1;j++){
					dp[i][i+b-1] = max(dp[i][i+b-1],dp[i][j] + dp[j+1][i+b-1]);
				}
			}
		}
		ll ans = 0;
		rep(i,n){
			rep(j,n){
				// cout << dp[i][j] << " ";
				ans = max(ans,dp[i][j]);
			}
			// cout << endl;
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cmath>
#include <iomanip>
#include <set>
#include <functional>

#define MOD (1000000007)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> tup;
const int MAX = 3000000;

long long fac[MAX], finv[MAX], inv[MAX];


ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}


// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

map<ll,ll> prime;//素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap

//O(√n)
//整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n){
    if(n<=1) return;
    ll l=sqrt(n);
   for(ll i=2;i<=(ll)(l);i++) { //
        if(n%i==0){
            prime_factorize(i);prime_factorize(ll(n/i));return;
        }
    }
    //mapでは存在しないkeyの場合も自動で構築される
    prime[n]++;return;
} 


int main() {
    while (true) {
        ll n;
        cin >> n;

        if(n == 0 ) break;

        vector<ll> v;

        for(ll i = 0; i < n; i++) {
            ll w;
            cin >> w;
            v.push_back(w);
        }

        vector<vector<ll>> dp(n+1, vector<ll>(n+1, 0));
        for(ll i = 0; i < n; i++) {
            dp[i][i+1] = 0;
        }
        for(ll i = 0; i < n - 1; i++) {
            dp[i][i+2] = abs(v[i] - v[i+1]) <= 1 ? 2 : 0;
        }

        for(ll diff = 3; diff <= n; diff++) {
            for(ll i = 0; i + diff <= n; i++) {
                //cout << "*** [" << i <<", " << i+diff << ")" << endl;
                if(dp[i+1][i+diff-1] == diff - 2 && abs(v[i] - v[i+diff-1]) <= 1) dp[i][i+diff] = diff;
                else 
                for(ll after = i+1; after <= i+diff-1; after++) {
                    // cout << "-> [" << i << ", " << after << ") + [" << after << ", " << i+diff << ")" << endl;
                    // cout <<  dp[i][after] + dp[after][i+diff] << endl;
                    dp[i][i+diff] = max(dp[i][i+diff], dp[i][after] + dp[after][i+diff]);
                  //  cout << "dp[" << i << "][" << i+diff << "] = " << dp[i][i+diff] << endl;
                }
            }
        }

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

    return 0;    
}