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 ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
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 long long INFLL = 1LL<<62;
const int INF = 1<<30;
const double PI=acos(-1);
int func(int l,int r,vector<vector<int>>&dp,vector<int>&w){
	//clog << l << " " << r<<endl; 
	if (dp[l][r] == -1)
	{
		chmax(dp[l][r],0);
		if(r!=l+1){
			for(int med=l;med<r;med++){
				if (func(l+1, r-1, dp, w) ==r-l-1){
					if(abs(w[l]-w[r])<=1){
						dp[l][r]=r-l+1;
					}
				}
					chmax(dp[l][r], func(l, med, dp, w) + func(med + 1, r, dp, w));
			}
		}else if(l!=r){
			if(abs(w[l]-w[r])<=1){
				dp[l][r]=2;
			}
		}
	}
//	clog << l << " " << r << " ->  " << dp[l][r] << endl;
	return dp[l][r];
}
int main(){
	int n=1;
	vector<vector<int>>dp(310,vector<int>(310,-1));
	vector<int>w(310,-2);
	while(1){
	rep(i,n){
		rep(j,n){
			dp[i][j]=-1;
		}
		w[i]=-2;
	}
	cin >>n;
	if(n==0){
		break;
	}
	rep(i,n){
		cin>>w[i];
	}

	cout<<func(0,n-1,dp,w)<<endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(pair<int, int> a, pair<int, int> b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};
vector<vector<ll>> dp(405,vector<ll>(405,-1));
vector<ll> w(400);
ll N;


ll daruma(ll l, ll r){

    if(dp[l][r] >= 0){
        return dp[l][r];
    } 
    if(abs(l - r) <= 1) return 0;

    ll ret = 0;  
    if(daruma(l+1,r-1) == r-l-2 && abs(w[r-1]-w[l]) <= 1){
        ret = r-l;
    }else{
        for(ll mid = l+1;mid <=r-1;mid++){
            chmax(ret,daruma(l,mid)+daruma(mid,r));
        }
    }

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

int main() {
    while(1){
    cin >> N;
    //dp.clear();
    rep(i,400){
        rep(j,400){
            dp[i][j] = -1;
        }
    }
    w.clear();
    if(N==0)break;
    rep(i,N){
        cin >> w[i];
    }
    //cout << "OK"<<endl;
    /*rep(i,N){
        dp[i][i] = 0;
    }*/
    
    ll ans = 0;

    

    cout <<daruma(0,N)<<endl;;
    }
    return 0;
    

}