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 _sel(_1, _2, x, ...) x
#define repn(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, ...) _sel(__VA_ARGS__, reps, repn)(i, __VA_ARGS__)
#define reppn(i, n) for (ll i = 1; i <= (n); i++)
#define repps(i, a, b) for (ll i = (a); i <= (b); i++)
#define repp(i, ...) _sel(__VA_ARGS__, repps, reppn)(i, __VA_ARGS__)
#define rrepn(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define rrep(i, ...) _sel(__VA_ARGS__, rreps, rrepn)(i, __VA_ARGS__)
#define rreppn(i, n) for (ll i = (n); i >= 1; i--)
#define rrepps(i, a, b) for (ll i = (b); i >= (a); i--)
#define rrepp(i, ...) _sel(__VA_ARGS__, rrepps, rreppn)(i, __VA_ARGS__)

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define rng(v) (v).begin(), (v).end()
#define rrng(v) (v).rbegin(), (v).rend()
#define _shv(v) for (auto _t1 : v) cerr << _t1 << " "; cerr << endl;
#define dg(x) cerr << #x << ": " << (x) << endl;
#define dgv(v) cerr << #v << ": "; _shv(v);
#define dgm(m) cerr << #m << ":" << endl; for (auto _t2 : m) { _shv(_t2); }

using namespace std;
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
const int DX[4] = {1, 0, -1, 0};
const int DY[4] = {0, 1, 0, -1};

template<typename T> inline void pt(T x) { cout << x << endl; }
template<typename T> inline void inv(T& v) { for (auto& t : v) cin >> t; }
template<typename T> inline int siz(T& x) { return x.size(); }
template<typename T> inline ll pow2(T n) { return 1ll << n; }
template<typename T, typename U> inline int bit(T x, U n) { return x >> n & 1; }
template<typename T, typename U>
inline bool umax(T& m, U x) { if (m < x) { m = x; return true; } return false; }
template<typename T, typename U>
inline bool umin(T& m, U x) { if (m > x) { m = x; return true; } return false; }


int calc(vvi& dp, vi& w, int l, int r) {
    if (dp[l][r] != -1) return dp[l][r];
    if (l == r) return dp[l][r] = 0;
    if (r - l == 1)  {
        if (abs(w[l] - w[r]) <= 1)
            return dp[l][r] = 2;
        else
            return dp[l][r] = 0;
    }
    int ret = 0;
    rep(i, l, r) {
        umax(ret, calc(dp, w, l, i) + calc(dp, w, i + 1, r));
    }
    if (calc(dp, w, l + 1, r - 1) == r - l - 1 && abs(w[l] - w[r]) <= 1)
        umax(ret, calc(dp, w, l + 1, r - 1) + 2);
    return dp[l][r] = ret;
}

int main() {
    while (true) {
        int n; cin >> n;
        if (n == 0) break;
        vi w(n); inv(w);
        vvi dp(n, vi(n, -1));
        calc(dp, w, 0, n - 1);
        pt(dp[0][n - 1]);
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
 
using namespace std;
 
#define PB push_back
#define MP make_pair
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
 
const int MOD = 1e9+7;
const int INF = 1e9;
 
int main() {
  while (true) {
    int n, ans = 0;
    bool canDrop[310][310];
    int dp[310][310];
    cin >> n;
    if (n == 0) break;
    vi c;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      c.PB(tmp);
    }
    for (int i = 0; i <= 300; i++) {
      for (int j = 0; j <= 300; j++) {
        canDrop[i][j] = false;
        dp[i][j] = 0;
      }
    }
    for (int len = 2; len <= n; len+=2) {
      for (int left = 0; left+len-1 < n; left++) {
        int right = left + len - 1;
        if (abs(c[left]-c[right])<=1) {
          if (len==2) {
            canDrop[left][right] = true;
            // cout << len << ": " << left << "_a_" << right << endl;
            continue;
          } else {
            if (canDrop[left+1][right-1]) {
              canDrop[left][right] = true;
              // cout << len << ": " << left << "_b_" << right << endl;
              continue;
            }
          }
        }
        for (int pos = left+1; pos < right; pos+=2) {
          if (canDrop[left][pos] && canDrop[pos+1][right]) {
            canDrop[left][right] = true;
            // cout << len << ": " << left << "_c_" << right << endl;
            break;
          }
        }
      }
    }
    for (int len = 2; len <= n; len++) {
      for (int left = 0; left+len-1 < n; left++) {
        int right = left + len;
        if (canDrop[left][right-1]) {
          dp[left][right] = len;
        }
        for (int pos = left+1; pos < right; pos++) {
          dp[left][right] = max(dp[left][right], dp[left][pos]+dp[pos][right]);
        }
        dp[0][n] = max(dp[0][n], dp[0][left]+dp[left][right]+dp[right][n]);
      }
    }
    cout << dp[0][n] << endl;
  }
  return 0;
}