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 <ccomplex>
#include <vector>
#include <cassert>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <list>
#include <assert.h>
#include <math.h>
#include <valarray>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <complex>
#include <list>
#include <time.h>
#include <stack>
#include <locale>
#include <clocale>
#include <ctype.h>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(),(c).end())
#define BACKSORT(c) sort((c).begin(),(c).end(),std::greater<LL>())
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)


int xcomp(vector<LL>& a, vector<LL>& b) {
	return a[0] < b[0];
}

#define XSORT(c) sort((c).begin(),(c).end(),xcomp)

#define ALL(x) (x).begin(),(x).end()
LL mod = 1000000007;

bool hantei(LL a, LL b) {
	if (a + 1 == b) {
		return true;
	}
	else
		if (a == b) {
			return true;
		}
		else
			if (a == b + 1) {
				return true;
			}
			else {
				return false;
			}
}

int main() {
	while (true)
	{
		LL  n;
		cin >> n;
		if (n == 0) {
			break;
		}

		vector<LL> num(n, 0);
		vector<LL> v(n, -9999);

		REP(i, n) {
			cin >> v[i];
		}
		REP(i, n - 1) {
			if (hantei(v[i], v[i + 1])) {
				num[i] = 2;
			}
		}

		bool flag = true;

		while (flag) {
			flag = false;

			REP(i, n) {
				LL hoge = num[i];
				if (i + num[i]<n) {
					if (hoge < num[i] + num[i + num[i]]) {
						hoge = num[i] + num[i + num[i]];
					}
				}
				if (i + 1<n) {
					if (i + num[i + 1] + 1<n) {
						if (hantei(v[i], v[i + num[i + 1] + 1])) {
							if (hoge < num[i + 1] + 2) {
								hoge = num[i + 1] + 2;
							}
						}
					}
				}
				if (hoge != num[i]) {
					flag = true;
					num[i] = hoge;
				}
			}
		}


		vector<LL> fuga(n * 3, 0);

		for (int i = n - 1; i >= 0; --i) {
			fuga[i] = max(fuga[i + 1], num[i] + fuga[i + num[i]]);
		}

		cout << fuga[0] << endl;
	}

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
    while(1){
        int n; cin>>n;
        if(n==0)break;
        int a[n];
        rep(i,n)cin>>a[i];
        int dp[n][n];
        rep(i,n)rep(j,n)dp[i][j]=-1;
        rep(i,n)dp[i][i]=0;
        rep(i,n-1){
            if(abs(a[i]-a[i+1])<=1)dp[i][i+1]=2;
            else dp[i][i+1]=0;
        }
        for(int i=2; i<n; i++){
            for(int j=0; j+i<n; j++){
                if(dp[j+1][j+i-1]==i-1){
                    if(abs(a[j]-a[j+i])<=1)dp[j][j+i]=i+1;
                    else dp[j][j+i]=i-1;                    
                }
                for(int k=0; k<i; k++){
                    dp[j][j+i]=max(dp[j][j+i], dp[j][j+k]+dp[j+k+1][j+i]);
                }
            }
        }
        cout<<dp[0][n-1]<<endl;
    }
    return 0;
}

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

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)(n-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    int n;
    while(cin >> n){
        if(n == 0) break;
        vi w(n);
        vvi dp(n, vi(n));
        rep(i,n) cin >> w[i];
        REP(W,2,n+1){
            rep(i,n){
                int j = i+W-1;
                if(j >= n) continue;
                if(dp[i+1][j-1] == W-2 && abs(w[i] - w[j]) <= 1) dp[i][j] = W;
                REP(k,i,j){
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);
                }
            }
        }
        cout << dp[0][n-1] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
#include <cmath>
 
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);++(i))
 
int main()
{
    int n;
    while(cin >> n , n)
    {
        vector<int> w(n,0);
        for(int i = 0;i < n;i++)
        {
            cin >> w[i];
        }
         
        vector<vector<int>> dp(n + 1,vector<int>(n + 1,0));
 
        for(int W = 2;W <= n;W++)
        {
            //left
            for(int l = 0;l < n;l++)
            {
                int r = l + W;
                if(r > n) continue;
 
                if(dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r - 1]) <= 1)
                    dp[l][r] = W;
 
                for(int mid = l;mid <= r;mid++)
                {
                    dp[l][r] = max(dp[l][r] , dp[l][mid] + dp[mid][r]);
                }
 
            }
 
        }
 
        cout << dp[0][n] << endl;
    }
    return 0;
}