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 int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

// 参考: https://drken1215.hatenablog.com/entry/2020/03/10/160500

ll n;
vll a;

ll solve(){
  vvll dp(n+1, vll(n+1, -n));
  for(ll i=0; i<=n; i++){
    dp[i][i]=0;
    if(i<n) dp[i][i+1]=0;
  }
  
  for(ll len=2; len<=n; len++){
    for(ll i=0; i+len<=n; i++){
      ll j=i+len; // 右端
      
      // dpの遷移は以下の2パターンが考えられる
      
      // 1) [i,j-1]の両端以外が消せて、両端が残るパターン
      // この場合は残った両端で消せるなら全消し、無理なら2個残る状態となる
      if(dp[i+1][j-1]==j-i-2){
        if(abs(a[i]-a[j-1])<=1) dp[i][j]=j-i;
        else chmax(dp[i][j],dp[i+1][j-1]);
      }
      
      // 2) あるkが存在して、[i,k), [k,j)を独立に操作して回数の最大値を求める場合
      // 2つの区間に分けてそれぞれが全消しできるパターンも当然存在する
      for(ll k=i+1; k<j-1; k++){
        chmax(dp[i][j], dp[i][k]+dp[k][j]);
      }
    }
  }
  
  return dp[0][n];
}

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  while(cin>>n, n){
    a.resize(n);
    rep(i,n) cin>>a[i];
    cout<<solve()<<endl;
  }
  
}

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

typedef long long ll;

struct ans
{
    int i;
    vector<vector<ll>> dp;
    vector<ll> w;
    ans(ll n): dp(n+1, vector<ll>(n+1,-1)), w(n){}
    ll rec(ll l, ll r){
        int i;
        if(dp.at(l).at(r) != -1){
            return dp.at(l).at(r);
        }
        if(abs(l-r) <= 1){
            return 0;
        }
        ll tmp = 0;
        if(abs(w.at(l) - w.at(r-1)) <= 1 && rec(l+1,r-1) == r-l-2){
            tmp = r - l;
        }else{
            for(i = l+1; i <= r-1;++i){
                tmp = max(tmp, rec(l,i) + rec(i,r));
            }
        }
        return dp.at(l).at(r) = tmp;
    }
};



int main()
{
    ll i,j;
    while(true){
        ll n;
        cin >> n;
        if(n == 0){
            return 0;
        }else{
            ans ans(n);
            for(i = 0;i < n;++i){
                cin >> ans.w.at(i);
            }
            cout << max((ll)0,ans.rec(0, n)) << endl;
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
typedef pair<int, int> P_i;
typedef pair<double,double> P_d;
#define SZ(x) ((int)(x).size())
#define rep(i,n) for(int i=0;i<(n);++i)
#define alrep(i,j,n) for(int i=0;i<(n);++i)for(int j=i+1;j<(n);++j)
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;int ans=0;
#define answer cout << ans << endl;
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; }
template<class T> using v = vector<T>;
template<class T> using vv = vector<vector<T>>;
const int MOD=1e9+7;
const long long INF = 1LL << 60;

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

signed main(){
  int n;
  while(cin >> n && n!=0){
    vv<int> dp(310,v<int>(310,-1));
    v<int> w(n,0);
    rep(i,n) cin >> w[i];
    cout << rec(0,n,dp,w) << endl;
  }
  return 0;
}