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<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; 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 - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 2; i <= n; i++) {
			for (int j = 0; j < n; j++) {
				int a1 = j, a2 = i + j - 1; if (a2 >= n)continue;
				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 > 1 && abs(a[a1 - 1] - a[a1 - 2]) <= 1)dp[a1 - 2][a2] = 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 = 0; i < n; i++) {
			for (int j = i + 1; j <= n; j++) {
				for (int k = i + 1; k <= j; k++) {
					int a1 = i, a2 = k, a3 = 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;
}

C++: #include <iostream>
#include <cmath>
using namespace std;

int main(){
  int n;
  while(cin >> n, n){
    int w[n];
    for(int i = 0; i < n; ++i)
      cin >> w[i];
    
    int dp[n][n];
    for(int i = 0; i < n; ++i){
      fill(dp[i],dp[i]+n,0);
    }
    
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
	if(j+i+1 > n-1) break;
	if(dp[j+1][j+i] == i && abs(w[j]-w[j+i+1]) < 2){
	  dp[j][j+i+1] = dp[j+1][j+i] + 2;
	}else{
	  for(int k = 0; k <= i; ++k){
	    dp[j][j+i+1] = max(dp[j][j+i+1],dp[j][j+k]+dp[j+k+1][j+i+1]);
	  }
	}
      }
    }
    //Debug
    /*
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
      cout << dp[i][j] << " ";
      }
      cout << endl;
    }

    */
    cout << dp[0][n-1] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

bool used[310][310];
bool dp[310][310];
vi w;

int dp2[310][310];
template<typename T> T chmax(T& a, const T& b){return a = max(a, b);}
template<typename T> T chmin(T& a, const T& b){return a = min(a, b);}
//[l, r) de tatakeruka
bool calc(int l, int r) {
	if(r - l <= 1) return false;
	if(used[l][r]) return dp[l][r];
	used[l][r] = true;
	if(r - l == 2) {
		return dp[l][r] = (abs(w[l] - w[r-1]) <= 1);
	}
	for(int m = l + 1; m < r; m++) {
		bool f = true;
		f &= calc(l, m);
		f &= calc(m, r);
		dp[l][r] |= f;
	}
	if(abs(w[l]-w[r-1]) <= 1) dp[l][r] |= calc(l+1, r-1);
	return dp[l][r];
}

int calc2(int l, int r) {
	if(dp2[l][r] != -1) return dp2[l][r];
	if(r - l <= 1) return dp2[l][r] = 0;
	if(dp[l][r]) return dp2[l][r] = r - l;
	int ret = 0;
	for(int m = l + 1; m < r; m++) {
		chmax(ret, calc2(l, m)+calc2(m, r));
	}
	return ret;
}

int main(){
	int n;
	while(cin >> n && n) {
		w.clear();
		w.resize(n);
		rep(i, n) cin >> w[i];
		rep(i, 310) rep(j, 310) dp[i][j] = used[i][j] = false, dp2[i][j] = -1;
		REP(r, 1, n+1) rep(l, r) calc(l, r);
		cout << calc2(0, n) << endl;
		
	}
	return 0;
}