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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int x[1000];
int dp[300][300];
int ans[300];

int C(int lb, int ub) {
	if (dp[lb][ub] != -1)return dp[lb][ub];
	if (ub - lb <= 0)return dp[ub][lb] = 0;
	else if (ub - lb == 1) {
		if (abs(x[lb] - x[ub]) <= 1)return dp[lb][ub] = 2;
		return dp[lb][ub] = 0;
	}
	int res = 0;
	if (abs(x[lb] - x[ub]) <= 1) {
		if (C(lb + 1, ub - 1) == ub - lb - 1) {
			res = C(lb + 1, ub - 1) + 2;
		}
	}
	FOR(mid, lb, ub) {
		res = max(res, C(lb, mid) + C(mid + 1, ub));
	}
	return dp[lb][ub] = res;
}

int main() {
	while (cin >> n&&n) {
		rep(i, 300)rep(j, 300) {
			dp[i][j] = -1;
		}
		rep(i, n)cin >> x[i];
		cout << C(0, n - 1) << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<deque>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];

int main()
{
	int n;
	
	while(cin >> n,n)
	{
		REP(i,310)REP(j,310)dp[i][j] = 0;
		
		vector<int> v(n);
		REP(i,n)cin >> v[i];
		
		REP(i,n-1)if(abs(v[i]-v[i+1]) < 2)dp[i][i+1] = 2;
		
		for(int k = 2;k < n;k++)
		{
			for(int i = 0;i+k < n;i++)
			{
				for(int j = 0;j < k;j++)
				{
					dp[i][i+k] = max(dp[i][i+k],dp[i][i+j]+dp[i+j+1][i+k]);
				}
				if(dp[i+1][i+k-1] == k-1)
				{
					int tmp = 0;
					if(abs(v[i]-v[i+k])<2)tmp = 2;
					dp[i][i+k] = max(dp[i][i+k],k-1+tmp);
				}
			}
		}
		
		cout << dp[0][n-1] << endl;
		
	}
	
	
	return 0;
}

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

int main(void){
  int n;
  cin >> n;
  
  while(n != 0){   
    vector<int> w(n);
    for(int i=0; i<n; i++)
      cin >> w[i];

    vector< vector<int> > dp(n, vector<int>(n));

    if(n == 1){
      cout << 0 << endl;
      cin >> n;
      continue;
    }

    for(int i=0; i<n-1; i++)
      if(abs(w[i] - w[i+1]) <= 1)
	dp[i][i+1] = 2;

    for(int k=3; k<n; k+=2){
      for(int i=0; i<n; i++){
	if(i+k >= n)
	  break;
	
	if(dp[i+1][i+k-1] == k-1)
	  if(abs(w[i] - w[i+k]) <= 1){
	    dp[i][i+k] = dp[i+1][i+k-1] + 2;
	    continue;
	  }

	int nmax = 0;
	for(int j=i+1; j<i+k; j+=2){
	  int temp = dp[i][j] + dp[j+1][i+k];
	  if(temp > nmax){
	    nmax = temp;
	  }
	}
	dp[i][i+k] = nmax;
      }
    }    

    if(n%2 != 0){
      int nmax = max(dp[1][n-1], dp[0][n-2]);
      for(int i=2; i<n-1; i+=2){
	int temp = dp[0][i-1] + dp[i+1][n-1];
	if(temp > nmax)
	  nmax = temp;
      }
      dp[0][n-1] = nmax;
    }
    
    cout << dp[0][n-1] << endl;

    cin >> n;
  }

  return 0;
}