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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

bool** table;
int** dp;
int N;

int recursive(int left,int right){

	if(dp[left][right] != -1)return dp[left][right]; //?????¢???????????´???
	if((right-left+1)%2 == 1){ //??????????\???°?????´???
		return dp[left][right] = max(recursive(left,right-1),recursive(left+1,right));
	}
	//if(table[left][(right-left+1)/2] == true)return dp[left][right] = right-left+1; //left???right?????¨????¶?????????´???
	//if(right == left+1 && table[left][1] == false)return dp[left][right] = 0;

	for(int i = left+1; i <= right-2; i++){
		dp[left][right] = max(dp[left][right],recursive(left,i)+recursive(i+1,right));
	}
	return dp[left][right];
}

int main(){

	int data[301];
	table = new bool*[301]; //table[left][right] = left???????§?????????????2*right??????????????¨?¶??????§??????????????????
	dp = new int*[301]; //?????????????????????????????§?¶????????????°
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		dp[i] = new int[301];
	}

	bool FLG;

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		if(N <= 1){
			printf("0\n");
			continue;
		}

		//?¶??????????????????????????????????
		for(int i = 0; i <= N; i++){
			for(int k = 0; k <= N/2; k++)table[i][k] = false;
			for(int k = 0; k <= N; k++){
				if(i == k)dp[i][k] = 0;
				else{
					dp[i][k] = -1;
				}
			}
		}

		for(int i = 1; i <= N; i++)scanf("%d",&data[i]);

		//????????????2???????¨?
		for(int i = 1; i <= N-1; i++){
			if(abs(data[i+1]-data[i]) <= 1){
				table[i][1] = true;
				dp[i][i+1] = 2;
			}else{
				dp[i][i+1] = 0;
			}
		}

		//?¶??????????????????????????¨????
		for(int length = 4; length <= N; length += 2){ //??????????????????
			for(int left = 1; left+length -1 <= N; left++){ //??????????????????
				FLG = false;

				if(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)FLG = true;//??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???

				if(!FLG){
					for(int i = 2; i <= length-2; i += 2){
						if(table[left][i/2] == true && table[left+i][(length-i)/2] == true){
							FLG = true;
							break;
						}
					}
				}
				if(FLG){
					table[left][length/2] = true;
					dp[left][left+length-1] = length;
				}
			}
		}

		printf("%d\n",recursive(1,N));
	}

	return 0;
}

C++: /*
 * aoj/1611_3rd/main.cpp
 */

// C++ 14
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring> // memset
#include <cassert>

using namespace std;

#define ll long long

#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)
template <class T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template <class T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }

ll N;
vector<ll> A(310);

ll dp[310][310]; // dp[l][r] := [l:r) で落とせた最大数

bool input() {
  cin >> N;
  if (N==0) return false;
  ll a;
  loop(n,0,N) {
    cin >> A[n];
  }
  return true;
}

ll receive() {
  memset(dp, 0, sizeof(dp));
  for (int len=2; len<=N; len++) {
    for (int l=0; l+len<=N; l++) {
      int r = l+len;
      if (len == 2) { dp[l][r] = 2 * (abs(A[l] - A[l+1]) <= 1); continue; }
      if (abs(A[l] - A[r-1]) <= 1 && dp[l+1][r-1] == len - 2) {
        chmax(dp[l][r], dp[l+1][r-1] + 2);
      }
      for (int k=l+1; k<r; k++) {
        chmax(dp[l][r], dp[l][k] + dp[k][r]);
      }
    }
  }
  return dp[0][N];
}

void solve() {
  cout << receive() << endl;
}

int main() {
  // cout.precision(15);
  while (input()) solve();
  return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

using namespace std;


int main() {
	ios::sync_with_stdio(false);
	cin >> N;
	while (N) {
		vector<int>num(N);
		for (int i = 0; i < N; i++)cin >> num[i];
		vector<vector<int>>dp(N, vector<int>(N,0));
		for (int i = 0; i < N - 1; i++) {
			if (abs(num[i] - num[i + 1]) < 2)dp[i][i + 1] = 2;
		}
		for (int i = 4; i <= N; i += 2) {
			for (int j = 0; i + j <= N; j++) {
				if (dp[j][i + j - 3] && dp[i + j - 2][i + j - 1]) {
					dp[j][i + j - 1] = i;
				}
				if (dp[j + 1][i + j - 2] && abs(num[j] - num[i + j - 1]) < 2) {
					dp[j][i + j - 1] = i;
				}
				//cout << j << " " << i + j - 1 << " " << dp[j][i + j - 1] << endl;
			}
		}
		vector<int>ret(N,0);
		ret[1] = dp[0][1];
		for (int i = 2; i < N; i++) {
			ret[i] = dp[0][i];
			for (int j = i - 1; j >= 1; j -= 2) {
				ret[i] = max(ret[i], ret[j-1] + dp[j][i]);
			}
		//	cout << i << " " << ret[i] << endl;
		}
		cout << ret[N - 1] << endl;
		cin >> N;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define repl(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,n) repl(i,0,n)
#define maxch(a,b) a=max(a,b)

int n;
int a[333];
int dp[333][333];

int main(int argc, char const *argv[])
{
  while(1){
    cin>>n;
    if(n==0)break;
    rep(i,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    rep(i,n-1)dp[i][i+2]=abs(a[i]-a[i+1])<=1?2:0;
    repl(len,3,n+1)rep(i,n-len+1){
      int j=i+len;
      repl(k,i,j){
        maxch(dp[i][j],dp[i][k]+dp[k][j]);
      }
      if(dp[i+1][j-1]==(j-1)-(i+1)&&abs(a[i]-a[j-1])<=1){
        maxch(dp[i][j],dp[i+1][j-1]+2);
      }
    }
    cout<<dp[0][n]<<endl;
  }
  return 0;
}