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++: #define TEST_MODE true

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a;             \
                               c > 0 ? i < b : i > b; \
                               i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define int long long

#ifdef LOCAL_ENV

#if TEST_MODE == true
const bool test = true;
#define dump(x) cerr << #x << " = " << (x) << endl
#else
const bool test = false;
#define dump(x) 
#endif

#else
const bool test = false;
#define dump(x) 
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = (int)1 << 60;
const ll INFL = (ll)1 << 60;
ll mod_n = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep)
{
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while (getline(ss, buffer, sep))
    {
        v.push_back(buffer);
    }
    return v;
}

template <class InputIterator>
int sum(InputIterator begin, InputIterator end)
{
    return accumulate(begin, end, 0ll);
}

void solve();

signed main()
{
    cout << fixed << setprecision(20);
    solve();
    while (true)
    {
        char s[MAX];
        if (scanf("%s", s) == EOF)
            break;
        int n = strlen(s);
        for (int i = n - 1; i > -1; i--)
        {
            ungetc(s[i], stdin);
        }
        solve();
    }
    return 0;
}

int n;
int w[MAX];
int done[305][305];
int dp[305][305];

void print_dp() {
    rep(i, n) {
        rep(j, n+1) {
            cout << setw(3) << dp[i][j];
        }
        cout << endl;
    }
}

int ans(int l, int r) {
    if (done[l][r]) {
        // if (test) {
        //     cout << "dp[" << l << "][" << r << "] : " << dp[l][r] << endl;
        // }
        return dp[l][r];
    }
    if (r - l == 0) return 0;
    if (r - l == 1) return 0; 
    if (l > r) return -INF;
    if (r > n) return -INF;
    if (r - l == 2) {
        done[l][r] = true;
        int res = 0;
        if (abs(w[l] - w[l+1]) < 2) res = 2;
        else res = 0;
        dp[l][r] = res;
        // if (test) {
        //     cout << "dp[" << l << "][" << r << "] : " << dp[l][r] << endl;
        // }
        return dp[l][r];
    }
    int res = 0;
    rep(width, n) {
        rep(width2, n) {
            rep2(i, l, r) {
                if (i+width+width2 > n) break;
                if (i+width+width2*2 > r) break;
                if (i+width2 == l && i+width+width2 == r) continue;
                if (i+width+width2*2 == l) continue;
                int tmp = 
                ans(l, i) 
                + ans(i+width2, i+width+width2) 
                + ans(i+width+width2*2, r);
                if (ans(i+width2, i+width+width2) == width) {
                    rep(j, width2) {
                        if (abs(w[i+width2-1-j] - w[i+width+width2+j]) < 2) {
                            tmp += 2;
                        }
                        else break;
                    }
                }
                res = max(res, tmp);
            }
        }
    }


    done[l][r] = true;
    dp[l][r] = res;
    // if (test) {
    //     cout << "dp[" << l << "][" << r << "] : " << dp[l][r] << endl;
    // }
    return dp[l][r];
}

void solve() {
    rep(i, 305) rep(j, 305) done[i][j] = false;
    rep(i, 305) rep(j, 305) dp[i][j] = 0;
    cin >> n;
    if (n == 0) exit(0);
    rep(i, n) cin >> w[i];
    cout << ans(0, n) << endl;
    if (test) print_dp();
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1611&lang=jp
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int n;
    while(cin >> n, n)
    {
        int dp[305][305] = {};
        vector<int> w(n);

        for(auto& _w : w)
        {
            cin >> _w;
        }

        for(int b = 2; b <= n; b++)
        for(int l = 0; l < n; l++)
        {
            int r = l + b - 1;
                if( r >= n )break;

                if( abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == r - l - 1)
                {
                    dp[l][r] = r - l + 1;
                }
                else
                for(int k = l; k < r; k++)                
                {
                    dp[l][r] = max( dp[l][r], dp[l][k] + dp[k + 1][r] );
                }
        }
        cout << dp[0][n - 1] << endl;
    }
    return 0;
}

C++: #include <iostream>

#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(x) cerr << #x << " = " << (x) << endl
#define SIZE 302

using namespace std;

int dp[SIZE][SIZE], w[SIZE];

int f(int j, int k) {
	int maxf = -1;
	if (dp[j][k] >= 0) return dp[j][k];
	// printf("f(%d, %d)\n", j, k);
	if (k - j <= 0) return dp[j][k] = 0;
	if (k - j == 1) {
		dp[j][k] = ((abs(w[j] - w[k])) <= 1 ? 2 : 0);
		/*if (abs(w[j] - w[k]) <= 1) {
			debug(j);
			debug(k);
		}*/
		/*debug(j);
		debug(k);
		debug(dp[j][k]);*/
		return dp[j][k];
	}
	if (k - j == 2) {
		dp[j][k] = max(f(j + 1, k), f(j, k - 1));
		return dp[j][k];
	}
	for (int i = j + 1; i + 1 < k; i++) {
		maxf = max(maxf, f(j, i) + f(i + 1, k));
		// debug(i);
		// debug(maxf);
	}
	dp[j][k] = max(f(j + 1, k - 1) + ((abs(w[j] - w[k]) <= 1 && f(j+1, k-1)==k-j-1) ? 2 : 0), maxf);
	/*if (abs(w[j] - w[k]) <= 1) {
		debug(j);
		debug(k);
		debug(dp[j][k]);
	}*/
	return dp[j][k];
}

int main() {
	while (1) {
		int n;

		cin >> n;
		if (n == 0) break;
		REP(i, n) cin >> w[i];
		REP(j, n) { REP(k, n) dp[j][k] = -1; }

		 cout << f(0, n - 1) << endl;
	}
	return 0;
}