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;

int SolveDaruma(const vector<int> &w) {
    const int n = w.size();
    vector<vector<int>> s(n, vector<int>(n, 0));

    for (int l = 0; l + 1 < n; ++l)
        if (abs(w[l] - w[l + 1]) <= 1) {
            s[l][l + 1] = 2;
        }

    for (int i = 4; i <= n; ++i) {
        for (int l = 0; l + i <= n; ++l) {
            const int r = l + i - 1;

            if (abs(w[l] - w[r]) <= 1 && s[l + 1][r - 1] == i - 2)
                s[l][r] = max(s[l][r], i);

            if (abs(w[l] - w[l + 1]) <= 1)
                s[l][r] = max(s[l][r], 2 + s[l + 2][r]);
            else
                s[l][r] = max(s[l][r], s[l + 2][r]);

            if (abs(w[r - 1] - w[r]) <= 1)
                s[l][r] = max(s[l][r], 2 + s[l][r - 2]);
            else
                s[l][r] = max(s[l][r], s[l][r - 2]);

            for (int m = l + 1; m + 1 < r; ++m) {
                if (abs(w[m] - w[m + 1]) <= 1)
                    s[l][r] = max(s[l][r], 2 + s[l][m - 1] + s[m + 2][r]);
                else
                    s[l][r] = max(s[l][r], s[l][m - 1] + s[m + 2][r]);
            }
        }
    }

    return s[0][n - 1];
}

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];

        cout << SolveDaruma(w) << '\n';
    }

    return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int n, w[300];
int memo[303];
int dp[303];
bool check(int i, int j) {
  if (j == i + 1) return true;
  if (i == j + 1) return true;
  if (i == j) return true;
  return false;
}
int getMax(int i) { // i - 消せる最大
  if (i >= n - 1) return 0;
  if (memo[i] >= 0) return memo[i];
  int ans = 0;
  if (check(w[i], w[i+1])) ans = 2 + getMax(i+2);
  int t = getMax(i+1);
  if (i + t + 1 < n && t > 0 && check(w[i], w[i + t + 1])) ans = max(ans, t+2+getMax(i+t+2));
  memo[i] = ans;
  return ans;
}
int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) break;
    for (int i = 0; i < n; i++) scanf("%d", &w[i]);
    for (int i = 0; i <= n + 2; i++) memo[i] = -1, dp[i] = 0;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (i == 2) dp[i] = max(dp[i], dp[i-2]);
      if (i >= 3) dp[i] = max(dp[i], max(dp[i-2], dp[i-3]));
      int t = getMax(i);
      if (i + t + 1 <= n + 2) dp[i + t + 1] = max(dp[i + t + 1], dp[i] + t);
      ans = max(ans, dp[i]);
    }
    ans = max(ans, dp[n]);
    ans = max(ans, dp[n + 1]);
    ans = max(ans, dp[n + 2]);
    printf("%d\n", ans);
  }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

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

		rep(i,n) rep(j,n) dp[i][j] = 0;

		range(w,2,n + 1){
			rep(i,n){
				int j = i + w - 1;
				if(j > n) break;
				if(abs(a[i] - a[j]) <= 1 and dp[i + 1][j - 1] + 2 == w){
					dp[i][j] = w;
				}
				range(k,i,j){
					dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
}

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

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

VI w;
int dp[301][301];

int rec(int l, int r){
    if(dp[l][r] != -1) return dp[l][r];

    if(r-l < 2) return dp[l][r] = 0;
    if(r-l == 2 && abs(w[r-1]-w[l])<2) return dp[l][r] = 2;
    if(r-l == 2 && abs(w[r-1]-w[l])>1) return dp[l][r] = 0;

    if(abs(w[r-1]-w[l])<2 && rec(l+1,r-1) == r-l-2) return dp[l][r] = r-l;
    int res = 0;
    FOR(i,l+1,r){
        chmax(res, rec(l,i)+rec(i,r));
    }
    return dp[l][r] = res;
}
int main(void){
    
    int n;
    cin >> n;
    while(n != 0){
        w.assign(n,0); REP(i,n) cin >> w[i];
        REP(i,n+1)REP(j,n+1) dp[i][j] = -1;
        int ans = rec(0,n);
        cout << ans << en;
        cin >> n;
    }
    return 0;
}