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>
#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x)/gcd(x,y)*(y))
#define isin(x, l, r) ((l) <= (x) and (x) < (r))
#define fcout(n) cout << fixed << setprecision(n)
#define rcout(n) cout << right << setw(n)
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define PI (acosl(-1))
#define EPS (1e-10)
#define INF LLONG_MAX
#define INF32 INT32_MAX
#define MOD 1000000007LL
using namespace std;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<pair<int, int>>;
using VTIII = vector<tuple<int, int, int>>;
using Complex = complex<double>;
template<typename T, template<typename> typename C = less> using PQ = priority_queue<T, vector<T>, C<T>>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a == b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a == b)){ a=b; return true; } return false; }


int rec(int l, int r, VVI& mem, const VI& w){
    if(r-l <= 1) return 0;
    int& res = mem[l][r];
    if(res != -1) return res;
    rep(i, l+1, r) chmax(res, rec(l, i, mem, w)+rec(i, r, mem, w));
    if(rec(l+1, r-1, mem, w) == r-1-l-1){
        if(abs(w[l]-w[r-1]) < 2) chmax(res, rec(l+1, r-1, mem, w)+2);
        else chmax(res, rec(l+1, r-1, mem, w));
    }
    return res;
}


signed main(){
    int n;
    while(cin >> n, n){
        VI w(n);
        rep(i, 0, n) cin >> w[i];
        VVI mem(n+1, VI(n+1, -1));
        cout << rec(0, n, mem, w) << endl;
    }

    return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    while (cin >> n, n)
    {
        vector<int> w(n);
        for (int i = 0; i < n; i++)
            cin >> w[i];
        vector<vector<int>> dp(n, vector<int>(n, 0));
        int ret = 0;
        for (int i = 0; i < n - 1; i++)
        {
            if (abs(w[i] - w[i + 1]) <= 1)
            {
                dp[i][i + 1] = 2;
                ret = 2;
            }
        }
        for (int i = 3; i <= n; i++)
        {
            for (int j = 0; j <= n - i; j++)
            {
                if (dp[j + 1][j + i - 2] == i - 2 && abs(w[j] - w[j + i - 1]) <= 1)
                {
                    dp[j][j + i - 1] = i;
                    ret = i;
                }
                else
                {
                    for (int k = j + 1; k <= j + i - 1; k++)
                    {
                        dp[j][j + i - 1] = max(dp[j][j + i - 1], dp[j][k - 1] + dp[k][j + i - 1]);
                    }
                    ret = max(ret, dp[j][j + i - 1]);
                }
            }
        }
        cout << ret << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<map>
#include<bitset>
using namespace std;
typedef long long ll;

#define rep(i, n) for(int i = 0;i < (n);i++)
#define repr(i, n) for(int i = (n);i >= 0;i--)
#define repf(i, m, n) for(int i = (m);i < (n);i++)

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;}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
//////////////////////////////////////////////////

ll dp[310][310];

int main() {
    while (1) {
        int n;
        cin >> n;
        if (!n) break;
        vector<int> w(n);
        rep(i,n) cin >> w[i];
        memset(dp, 0, sizeof(dp));

        repf(width, 2, n+1) rep(left,n) {
            int right = left + width - 1;
            if (right >= n) continue;

            if (dp[left+1][right-1] == width-2 && abs(w[left]-w[right]) <= 1) {
                dp[left][right] = width;
            }

            repf(mid, left, right) {
                chmax(dp[left][right], dp[left][mid]+dp[mid+1][right]);
            }
        }
        cout << dp[0][n-1] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<tuple>
#include<algorithm>
#include<cmath>
using namespace std;
vector<tuple<int, int, int>>vec; int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; vec.clear(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; }
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { vec.push_back(make_tuple(j - i, i, j)); } }
		sort(vec.begin(), vec.end());
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 0; i < vec.size(); i++) {
			int a1 = get<1>(vec[i]), a2 = get<2>(vec[i]);
			if (dp[a1][a2] == 0)continue;
			if (a2 < n - 2 && abs(a[a2 + 1] - a[a2 + 2]) <= 1)dp[a1][a2 + 2] = 1;
			if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)dp[a1 - 1][a2 + 1] = 1;
		}
		for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[i][j] == 1)dp[i][j] = j - i + 1; } }
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				for (int k = 0; k < n; k++) {
					int a1 = k, a2 = k + i, a3 = k + i + j; if (a3 > n)continue;
					dp[a1][a3 - 1] = max(dp[a1][a3 - 1], dp[a1][a2 - 1] + dp[a2][a3 - 1]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}