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;

using namespace std;

struct Info{
	Info(){
		loc = div = 0;
	}
	int loc,div;
};

int N,maximum;

void recursive(int table[],int num,int SIZE){

	maximum = max(maximum,2*num);

	int max_chain = 0,tmp_chain,next_index = 0;
	Info nextTable[SIZE];

	for(int i = 1; i <= SIZE-1; i++){
		if(abs(table[i] - table[i-1]) <= 1){
			if(i == SIZE-1){
				if(max_chain == 0){
					nextTable[next_index].loc = SIZE-1;
					nextTable[next_index++].div = 0;
				}
			}else{
				tmp_chain = 0;
				for(int k = 1;(i-1)-k>=0 && i+k <= SIZE-1;k++){
					if(abs(table[(i-1)-k] - table[i+k]) <= 1){
						tmp_chain++;
					}
					else{
						break;
					}
				}
				if(tmp_chain > max_chain){
					max_chain = tmp_chain;
					next_index = 0;
					nextTable[next_index].loc = i;
					nextTable[next_index++].div = tmp_chain;
				}else if(tmp_chain == max_chain){
					nextTable[next_index].loc = i;
					nextTable[next_index++].div = tmp_chain;
				}
			}
		}
	}

	if(next_index == 0)return;

	for(int i = 0; i < next_index;i++){ //?????????????????¨???????????¨???????????????????????????
		int work[SIZE],new_table[SIZE],next_size = 0;
		for(int k = 0; k < SIZE; k++)work[k] = table[k];
		work[nextTable[i].loc-1] = -1;
		work[nextTable[i].loc] = -1;

		for(int k = 1; k <= nextTable[i].div; k++){
			work[nextTable[i].loc-1-k] = -1;
			work[nextTable[i].loc + k] = -1;
		}

		for(int k = 0; k < SIZE; k++){
			if(work[k] != -1){
				new_table[next_size++] = work[k];
			}
		}
		recursive(new_table,num+nextTable[i].div+1,next_size);
		break; //??????1??????????????????
	}
}

void func(){

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

	if(N == 1){
		printf("0\n");
		return;
	}else if(N == 2){
		if(abs(first[0] - first[1] <= 1)){
			printf("1\n");
		}else{
			printf("0\n");
		}
		return;
	}

	recursive(first,0,N);

	printf("%d\n",maximum);
}


int main(){

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

		func();
	}

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <regex>
#include <numeric>
#include <queue>
#include <set>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

int main(int argc, char const *argv[])
{
  while (true)
  {
    ll n;
    cin >> n;
    if (n == 0)
      break;
    vector<ll> w(n + 1);
    REP(i, n)
    {
      cin >> w[i + 1];
    }
    vector<vector<ll>> dp(n + 2, vector<ll>(n + 2));
    REP(i, n)
    {
      for (ll b = 1; b <= n - i; b++)
      {
        ll t = b + i;
        if (dp[b + 1][t - 1] == t - b - 1 && abs(w[b] - w[t]) <= 1)
          dp[b][t] = dp[b + 1][t - 1] + 2;
        ll maxi = 0;
        for (ll mid = b; mid < t; mid++)
        {
          dp[b][t] = max(dp[b][t], dp[b][mid] + dp[mid + 1][t]);
        }
      }
    }
    cout << dp[1][n] << endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n;
	while (cin >> n, n) {
		vi num(n);
		REP(i, n)
			cin >> num[i];
		vector<vector<bool>> able(n, vb(n, false));
		for (int i = 2;i < n + 1;i += 2) {
			for (int j = 0;j + i - 1 < n;j++) {
				if (i == 2) {
					if (abs(num[j] - num[j + i - 1]) <= 1)
						able[j][j + i - 1] = true;
				}
				else {
					if (abs(num[j] - num[j + i - 1]) <= 1 && able[j + 1][j + i - 2])
						able[j][j + i - 1] = true;
					REP(k, i / 2 - 1)
						if (able[j][j + k * 2 + 1] && able[j + k * 2 + 2][j + i - 1])
							able[j][j + i - 1] = true;
				}
			}
		}
		vvi DP(n, vi(n, 0));
		REP(i, n)
			REP(j, n)
			if (able[i][j])
				DP[i][j] = j - i + 1;
		for (int i = 3;i < n + 1;i++) {
			for (int j = 0;j + i - 1 < n;j++)
				REP(k, i - 1)
				DP[j][j + i - 1] = max(DP[j][j + i - 1], DP[j][j + k] + DP[j + k][j + i - 1]);
		}
		cout << DP[0][n - 1] << endl;
	}
}