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;

int N,maximum;

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

	maximum = max(maximum,2*num);

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

	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++] = SIZE-1;
				}
			}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++] = i;
				}else if(tmp_chain == max_chain){
					nextTable[next_index++] = i;
				}
			}
		}
	}

	if(next_index == 0)return;

	for(int i = 0; i < next_index;i++){
		int new_table[SIZE-2],next_size = 0;
		for(int k = 0; k < SIZE; k++){
			if(k != nextTable[i] && k != nextTable[i]-1){
				new_table[next_size++] = table[k];
			}
		}
		recursive(new_table,num+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;

int dp[302][302];

int main(){
    int n;
    while(cin >> n, n != 0){
        vector<int> v;
        for(int i=0;i<n;++i){
            int w;
            cin >> w;
            v.push_back(w);
        }

        for(int i=0;i<=300;++i)
            for(int j=0;j<=300;++j)
                dp[i][j] = 0;

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

        for(int k=2;k<n;++k){
            for(int i=0;i<n;++i){
                if(i+k >= n)break;
                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 && abs(v[i]-v[i+k]) <= 1)
                    dp[i][i+k] = k + 1;
                dp[i][i+k] = max(dp[i][i+k], dp[i+1][i+k-1]);
            }
        }

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

    return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
const int MOD=1e9+7;


int main() {
    ios_base::sync_with_stdio(false);

    while (1) {
        int n;
        cin >> n;
        if (n==0) break;
        vector<int> a(n);
        rep(i,n) cin >> a[i];
        vector<vector<int>> dp(n,vector<int>(n));
        repr(i,1,n) {
            invrep(j,i) {
                if (dp[j+1][i-1]==(i-1)-(j+1)+1 && abs(a[j]-a[i])<=1) dp[j][i]=i-j+1;
                repr(k,j,i) {
                    dp[j][i]=max(dp[j][i],dp[j][k]+dp[k+1][i]);
                }
            }
        }
        cout << dp[0][n-1] << endl;
    }
    

    return 0;   
}

C++: #include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge
{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
// using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  while (1)
  {
    cin >> n;
    if (n == 0)
      break;
    vector<int> w(n);
    for (auto &it : w)
      cin >> it;
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
    for (int W = 2; W <= n; W++)
    {
      // left
      for (int l = 0; l < n; l++)
      {
        int r = l + W;
        if (r > n)
          continue;
        if (dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r - 1]) <= 1)
          dp[l][r] = W;

        for (int mid = l; mid <= r; mid++)
        {
          dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
        }
      }
    }
    ll ans = dp[0][n];
    cout << ans << endl;
  }
  return 0;
}