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<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;
 
ll dp[302][302];
bool f[302][302];
vector<ll>a;
ll n;

ll Abs(ll x,ll y){
    if(x>=y)return x-y;
    else return y-x;
}


ll rec(ll l,ll r){
    if(f[l][r])return dp[l][r];
    if(l==r)return dp[l][r]=0;
    if(l>r)return dp[l][r]=0;
    if(l<=0)return 0;
    if(r>n)return 0;
    if(r-l==1){
        f[l][r]=true;
        if(Abs(a[l],a[r])<=1)return dp[l][r]=2;
        else return dp[l][r]=0;
    }
    if(Abs(a[l],a[r])<=1){
        dp[l+1][r-1] = rec(l+1,r-1);
        f[l+1][r-1]=true;
        if(dp[l+1][r-1]==r-l-1){
            f[l][r]=true;
            return dp[l][r]=r-l+1;
        }
    }
    ll ans = 0;
    for(ll k=l;k<r;k++){
        ans = max(ans,rec(l,k)+rec(k+1,r));
    }
    f[l][r]=true;
    return dp[l][r]=ans;
}


int main(void){
    while(true){
        for(ll i=0;i<302;i++){
            for(ll j=0;j<302;j++){
                dp[i][j]=0;
                f[i][j]=false;
            }
        }
        cin>>n;
        if(n==0)break;
        a.resize(n+1,0);
        for(ll i=0;i<n;i++){
            ll x;
            cin>>x;
            a[i+1]=x;
        }
        ll ans = 0;
        cout<<rec(1,n)<<endl;
        /*for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cout<<dp[i][j]<<" ";
            }
            cout<<endl;
        }*/
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    while(1){
        int n;
        cin>>n;
        if(n==0){
            break;
        }
        int w[310];
        for(int i=0;i<n;i++){
            cin>>w[i];
        }
        int dp[310][310];
        int dp2[310][310];
        //dp[i][j] range(i,j)
        //0<=i<=j<=n
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
                dp[i][j]=0;
                dp2[i][j]=0;
            }
        }
        for(int a=0;a<=n;a++){
            for(int b=0;b+a<=n;b++){
                int i,j;
                i=b;j=b+a;
                if (a==0){
                    dp[i][j]=1;
                }
                for(int k=i;k<=j;k++){
                    if (dp[i][k]==1 && dp[k][j]==1){
                        dp[i][j]=1;
                    }
                }
                if(dp[i][j]==1){
                    if (i-2>=0 && abs(w[i-2]-w[i-1])<=1){
                        dp[i-2][j]=1;
                    }
                    if (j+2<=n && abs(w[j]-w[j+1])<=1){
                        dp[i][j+2]=1;
                    }
                    if (i-1>=0 && j+1<=n && abs(w[i-1]-w[j])<=1){
                        dp[i-1][j+1]=1;
                    }
                }
                //cout<<dp[i][j]<<" ";
            }
            //cout<<endl;
        }
        for(int a=0;a<=n;a++){
            for(int b=0;b+a<=n;b++){
                int i,j;
                i=b;j=b+a;
                if (dp[i][j]==1){
                    dp2[i][j]=j-i;
                }
                for(int k=i;k<=j;k++){
                    dp2[i][j]=max(dp2[i][j],dp2[i][k]+dp2[k][j]);
                }
                //cout<<dp2[i][j]<<" ";
            }
            //cout<<endl;
        }
        cout<<dp2[0][n]<<endl;
    }
}

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

ll N;
vector<vector<ll>> dp;
vector<ll> W;

ll dfs(ll l, ll r) {
    if (dp[l][r] != -1) {
        return dp[l][r];
    }
    if (abs(r - l) == 1) {
        return dp[l][r] = 0;
    } else if (abs(r - l) == 2) {
        if (abs(W[l] - W[r - 1]) <= 1) {
            return dp[l][r] = 2;
        } else {
            return dp[l][r] = 0;
        }
    }

    ll res = 0;

    if (abs(W[l] - W[r - 1]) <= 1 && dfs(l + 1, r - 1) == abs(r - 1 - (l + 1))) {
        res = r - l;
    }

    for (ll k = l + 1; k < r; k++) {
        res = max(res, dfs(l, k) + dfs(k, r));
    }
    return dp[l][r] = res;
}

void solve() {
    while (true) {
        cin >> N;
        if (N == 0) break;
        dp.assign(N + 1, vector<ll>(N + 1, -1));
        W.assign(N, 0);
        rep(i, N) cin >> W[i];
        cout << dfs(0, N) << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}