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 namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    while (cin >> n, n) {
        vector<ll> a(n); cin >> a;
        vector<vector<ll>> dp(n, vector<ll>(n+1, 0));
        auto check = [&](const ll idx1, const ll idx2) {
            return abs(a[idx1] - a[idx2]) <= 1;
        };
        rep(i, n-1) {
            if (check(i, i+1)) dp[i][i+2] = 2;
        }
        rep(w, 3, n+1) rep(l, n) {
            ll r = l + w;
            if (r > n) break;
            rep(m, l+1, r) {
                chmax(dp[l][r], dp[l][m] + dp[m][r]);
            }
            if (check(l, r-1) && dp[l+1][r-1] == r-l-2) {
                chmax(dp[l][r], dp[l+1][r-1]+2);
            }
        }
        cout << dp[0][n] << endl;
    }
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n;
	while (cin >> n, n) {
		vi num(n);
		REP(i, n)
			cin >> num[i];
		vector<vector<bool>> able(n, vb(n, false));
		for (int i = 2;i < n + 1;i += 2) {
			for (int j = 0;j + i - 1 < n;j++) {
				if (i == 2) {
					if (abs(num[j] - num[j + i - 1]) <= 1)
						able[j][j + i - 1] = true;
				}
				else {
					if (abs(num[j] - num[j + i - 1]) <= 1 && able[j + 1][j + i - 2])
						able[j][j + i - 1] = true;
					REP(k, i / 2 - 1)
						if (able[j][j + k * 2 + 1] && able[j + k * 2 + 2][j + i - 1])
							able[j][j + i - 1] = true;
				}
			}
		}
		vvi DP(n, vi(n, 0));
		REP(i, n)
			REP(j, n)
			if (able[i][j])
				DP[i][j] = j - i + 1;
		for (int i = 3;i < n + 1;i++) {
			for (int j = 0;j + i - 1 < n;j++)
				REP(k, i)
				DP[j][j + i - 1] = max(DP[j][j + i - 1], DP[j][j + k] + DP[j + k+1][j + i - 1]);
		}
		cout << DP[0][n - 1] << endl;
	}
}

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

void daruma(pair<int, vector<int>> dataset)
{
  int n;
  vector<int> w;
  tie(n, w) = dataset;
  vector<vector<int>> dp(n, vector<int>(n, 0));
  for (int W = 2; W <= n; W++)
  {
    for (int l = 0; l < n; l++)
    {
      int r = l + W - 1;
      if (r >= n)
        continue;

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

int main()
{
  vector<pair<int, vector<int>>> dataset;
  while (1)
  {
    int n;
    cin >> n;
    if (n < 1)
      break;
    vector<int> w(n);
    for (int i = 0; i < n; ++i)
    {
      cin >> w[i];
    }
    dataset.push_back(make_pair(n, w));
  }
  for (int i = 0; i < dataset.size(); ++i)
  {
    daruma(dataset.at(i));
  }
}