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;
typedef pair<int,int> P;
int main(){
  int n;
  while(cin>>n,n){
    int w[n],i,j,k=0;
    for(i=0;i<n;i++) cin>>w[i];
    bool p[n][n],u[n][n];
    memset(p,0,sizeof(p));
    memset(u,0,sizeof(u));
    queue<P> q;
    for(j=0;j+1<n;j++) if(abs(w[j+1]-w[j])<2) p[j][j+1]=1,q.push(P(j,j+1));
    while(!q.empty()){
      int l=q.front().first,r=q.front().second;q.pop();
      if(u[l][r]) continue;
      u[l][r]=1;
      if(l>0&&r+1<n&&abs(w[l-1]-w[r+1])<2) p[l-1][r+1]=1,q.push(P(l-1,r+1));
      for(i=r+1;i<n;i++) if(p[r+1][i]) p[l][i]=1,q.push(P(l,i));
      for(i=0;i<l;i++) if(p[i][l-1]) p[i][r]=1,q.push(P(i,r));
    }	  
    int dp[n+1];
    memset(dp,0,sizeof(dp));
    for(i=0;i<=n;i++)
      for(j=1;i+j<=n;j++)
	dp[i+j]=max(dp[i+j],dp[i]+p[i][i+j-1]*j);
    cout<<dp[n]<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int main()
{
	for(int n; ; )
	{
		scanf("%d", &n);
		if(!n)
		{
			return 0;
		}
		vector<int> a(n);
		for(int i = 0; i < n; ++i)
		{
			scanf("%d", &a[i]);
		}
		vector<vector<int>> dp(n, vector<int>(n + 1, 0));
		for(int i = n - 1; i >= 0; --i)
		{
			for(int j = i + 1; j <= n; ++j)
			{
				if(i + 2 <= j && abs(a[i] - a[j - 1]) <= 1 && dp[i + 1][j - 1] == j - i	 - 2)
				{
					dp[i][j] = j - i;
				}
				for(int k = j + 1; k <= n; ++k)
				{
					dp[i][k] = max(dp[i][k], dp[i][j] + dp[j][k]);
				}
			}
		}
		printf("%d\n", dp[0][n]);
	}
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int n,w[300],dp[300][300];
    scanf("%d",&n);
    while(n){
        for(int i=0;i<n;i++){
            scanf("%d",&w[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                dp[i][j]=0;
            }
        }
        for(int k=1;k<n;k++){
            for(int i=0;i+k<n;i++){
                if(dp[i+1][i+k-1]==k-1&&max(w[i],w[i+k])-min(w[i],w[i+k])<=1){
                    dp[i][i+k]=k+1;
                }
                else{
                    for(int j=i;j<i+k;j++){
                        dp[i][i+k]=max(dp[i][i+k],dp[i][j]+dp[j+1][i+k]);
                    }
                }
            }
        }
        printf("%d\n",dp[0][n-1]);
        scanf("%d",&n);
    }
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}

ll gcd(ll a, ll b){if(b==0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}

bool take[310][310];
ll dp[310][310]; // 数え上げる部分


ll solver(){
    ll N;
    vector<ll> daruma;
    cin >> N;
    
    rep(i,N){
        ll inp;
        cin >> inp;
        daruma.pb(inp);
    }
    
    // takeの初期化
    for(ll i = 0; i < N; i++){
        for(ll j = i+1; j< N; j++){
            take[i][j] = false;
            if((j == i+1) && abs(daruma[j]- daruma[i]) <= 1){
                take[i][j] = true;
            }
        }
    }
    
    // takeの更新
    for(ll i = 0 ; i < N ; i++){ // 長さ
        for(ll j = 0; j < N; j++){// start
            ll len = i+1;
            ll s = j;
            ll t = j+i;
            
            if(t >= N) break;
            
            // 左右の二つを足した結果。
            for(ll k = s+1; k < t; k++){
                if(take[s][k] && take[k+1][t]) {
                    take[s][t] = true;
                    break;
                }
            }
            
            // 前後伸びるやつ
            if((s-1 < 0) || (t+1 >= N)) continue;
            if(take[s][t]  && (abs(daruma[s-1] - daruma[t+1]) <= 1)) take[s-1][t+1] = true;
        }
    }
    
    return N;
}



void answer(ll N){
    if(N == 0) return;
    
    // dp初期化
    for(ll i = 0 ; i < N; i++){
        for(ll j = i+1; j < N; j++){
            if(take[i][j]) dp[i][j] = j-i+1;
            else dp[i][j] = 0 ;
        }
    }

    for(ll i = 0 ; i < N; i++){
        for(ll j = i+1; j < N; j++){
            for(ll k = i; k < j ; k++){
                dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);
            }
        }
    }

    cout << dp[0][N-1] << endl;
    return ;
}


void showtake(ll N){
    cout << "------------take------------" << endl;
    rep(i, N){
        rep(j, N){
            if(take[i][j]) cout << 1 << " ";
            else cout << 0 << " ";
        }
        cout << endl;
    }
    cout << "----------------------------" << endl;
}

void showanswer(ll N){
    cout << "------------answer----------" << endl;
    rep(i, N){
        rep(j, N){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }
    cout << "----------------------------" << endl;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N;
    while(1){
        N = solver();
        //showtake(N);
        answer(N);
        //showanswer(N);
        if(N == 0) return 0;
    }
    
    
    //cout << fixed << setprecision(16) << ans << endl;
    return 0;
}