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;
int main(){
  while (1){
    int n;
    cin >> n;
    if (n == 0){
      break;
    }
    vector<int> w(n);
    for (int i = 0; i < n; i++){
      cin >> w[i];
    }
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
    for (int d = 1; d <= n; d++){
      for (int i = 0; i <= n - d; i++){
        int j = i + d;
        if (d % 2 == 1){
          for (int k = i; k <= j; k++){
            dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
          }
        } else {
          for (int k = i; k <= j; k++){
            dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
          }
          if (abs(w[j - 1] - w[i]) <= 1 && dp[i + 1][j - 1] == j - i - 2){
            dp[i][j] = max(dp[i][j], dp[i + 1][j - 1] + 2);
          }
        }
      }
    }
    cout << dp[0][n] << endl;
  }
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define ll long long
using namespace std;
//typedef vector<unsigned int>vec;
//typedef vector<ll>vec;
//typedef vector<vec> mat;
typedef pair<int, int> P;
typedef pair<ll,ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll  MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
//template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    while(cin >> N, N){
        vector<int> w(N);
        REP(i,N) cin >> w[i];
        // REP(i,N) cout << w[i] << " ";
        // cout << endl;
        vector<vector<int>> dp(N+1, vector<int>(N+1, -1));
        auto solve = [&](auto && self, int l, int r) -> int{
            if(l >= r) return 0;
            int &res = dp[l][r];
            if(res != -1) return res;
            for(int i=l;i<r;i++){
                res = max(res, self(self, l, i) + self(self, i+1, r));
            }
            if(abs(w[l] - w[r]) <= 1){
                if(self(self, l + 1, r - 1) == max(0, r - l - 1)) res = r - l + 1;
            }
            return res;
        };

        cout << solve(solve, 0, N-1) << endl;;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for(ll i = ll(a); i < ll(b); i++)
#define rer(i, a, b) for(ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
using namespace std;
typedef long long int ll;
typedef pair <int, int> P;

const ll MOD=1000000007;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    for(;;){
        int n;
        cin>>n;
        if(!n) return 0;
        int w[310];
        rep(i,0,n) cin>>w[i];
        int dp[310][310];
        rep(i,0,n){
            dp[i][i+1]=0;
            dp[i][i+2]=0;
            if(abs(w[i]-w[i+1])<=1) dp[i][i+2]=2;
        }
        rep(j,3,n+1){
            rep(i,0,n+1-j){
                dp[i][i+j]=dp[i][i+j-1];
                if(abs(w[i]-w[i+j-1])<=1&&dp[i+1][i+j-1]==j-2) dp[i][i+j]=j;
                rep(k,i+1,i+j){
                    if(abs(w[k]-w[i+j-1])<=1&&dp[k+1][i+j-1]==i+j-k-2) dp[i][i+j]=max(dp[i][i+j], int(i+j-k+dp[i][k]));
                }
            }
        }
        cout <<dp[0][n]<<"\n";
    }
}

C++: // g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
//typedef ll Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int n;
vi in;
int dp[310][310];
int f(int l,int r){
	if(dp[l][r]+inf)return dp[l][r];
	if(r-l<2)return dp[l][r]=0;
	int out=-inf;
	loop(i,l,r-1)if(abs(in[i]-in[i+1])<2){
		out=max(out,f(l,i)+2+f(i+2,r));
	}
	int t=f(l+1,r-1);
	if(t==r-l-2){
		if(abs(in[l]-in[r-1])<2)out=max(out,r-l);
	}
	out=max(out,f(l+1,r));
	out=max(out,f(l,r-1));
//	cout<<l<<" "<<r<<" "<<out<<endl;
	return dp[l][r]=out;
}
int main(){
	while(cin>>n,n){
		in=vi(n);
		rep(i,n)cin>>in[i];
		rep(i,310)rep(j,310)dp[i][j]=-inf;
		cout<<f(0,n)<<endl;;
	}
}