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);
	}
}

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 <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1 ; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1 ; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9+100;
const bool debug = 0;
/***************************************/

int N;
int a[300];
int dp1[300][301]; // [l, r)
int dp2[301];

int main() {
	while (cin >> N, N) {
		REP(i, N) scanf("%d", a + i);
		
		memset(dp1, 0, sizeof(dp1));
		REP(i, N - 1) if (abs(a[i] - a[i + 1]) <= 1) dp1[i][i + 2] = 2;
		FOR(w, 4, N + 1) {
			REP(i, N - w + 1) {
				int l = i, r = i + w; // [l, r)
				FOR(k, l + 1, r) { // [l, k), [k, r)
					if (dp1[l][k] && dp1[k][r]) dp1[l][r] = w;
				}
				if (abs(a[l] - a[r - 1]) <= 1 && dp1[l+1][r-1]) dp1[l][r] = w;
			}
		}
		
		memset(dp2, 0, sizeof(dp2));
		REP(i, N) {
			FOR(j, i + 1, N + 1) chmax(dp2[j], dp2[i] + dp1[i][j]);
			chmax(dp2[i + 1], dp2[i]);
		}
		
		if (debug) printf("@ ");
		cout << dp2[N] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int main() {
    int n;
    vector<int> w;
    while(cin >> n, n){
        w.clear();
        w.push_back(-IINF/2);
        REP(i,n){
            int wi;
            cin >> wi;
            w.push_back(wi);
        }
        w.push_back(IINF/2);
        bool update = true;
        while(update){
            while(update){
                update = false;
                for(int i=int(w.size())-3;i>=1;i--){
                    if(abs(w[i]-w[i+1])<=1 && abs(w[i-1]-w[i+2])<=1){
                        w.erase(w.begin()+i,w.begin()+i+2);
                        update = true;
                    }
                }
            }
            for(int i=int(w.size())-2;i>=1;i--){
                if(abs(w[i]-w[i+1])<=1){
                    w.erase(w.begin()+i,w.begin()+i+2);
                    update = true;
                }
            }
        }
        cout << n - int(w.size()) + 2 << endl;
    }
    return 0;
}

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

int main() {
    int N;
    while (cin >> N, N) {
        vector<int> v(N);
        for (auto& i : v) cin >> i;

        vector<vector<int>> dp(N+1, vector<int>(N));
        for (int i = 0; i < N; i++) {
            if (abs(v[i] - v[i+1]) < 2) {
                dp[2][i] = 1;
            }
        }
        for (int i = 4; i <= N; i += 2) {
            for (int j = 0; j + i <= N; j++) {
                if (dp[i-2][j+1] && abs(v[j] - v[j + i - 1]) < 2) {
                    dp[i][j] = 1;
                } else {
                    vector<int> flag(i+1);
                    for (int k = 2; j + k < j + i; k += 2) {
                        if (dp[k][j] && dp[i - k][j + k]) {
                            dp[i][j] = 1;
                        }
                    }
                }
            }
        }

        vector<int> sum(N+1);
        for (int i = 0; i < N; i++) {
            if (i) sum[i] = max(sum[i], sum[i-1]);
            for (int j = 2; j <= N; j += 2) {
                if (dp[j][i]) {
                    sum[i+j] = max(sum[i+j], sum[i] + j);
                }
            }
        }

        cout << max(sum[N-1], sum[N]) << endl;
    }

    return 0;
}