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 <functional>
#include<numeric>
#include<cmath>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

/*int darma_max(vector<vector<int> > dp, int ){
    

}*/

int main(){
    //cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	//cout << fixed << setprecision(15);
    vector<int> ans;
    while(true){
        int N; cin >> N;
        //ans.resize(N);
        if(N == 0){
            REP(i,ans.size()){
                cout << ans[i] << endl;
            }
            return 0;
        }
        else{
            vector<vector<int> >dp(N+1,vector<int>(N+1,0));
            vector<int> darma(N);
            vector<bool> flag(N,false);
            //flag.resize(N,false);
            /*REP(i,N+1){
                dp[i][i] = true; //半開区間？
            }*/
            REP(i,N){cin >> darma[i];}

            for(int w = 2; w <= N; w++){
                for(int left = 0; left + w <= N; left++){
                    int right = left + w;
                    if((dp[left+1][right-1] == w-2) && (abs(darma[left] - darma[right-1]) <= 1)){
                        dp[left][right] = w;
                        //flag[left] = true; flag[right-1] = true;
                    }
                    else{
                        for(int mid = left + 1; mid < right; mid++){
                            /*if(dp[left][mid] && dp[mid][right]){
                                dp[left][right] = true;
                                break;
                            }*/
                            dp[left][right] = max(dp[left][right], dp[left][mid] + dp[mid][right]);
                        }
                    }
    
                }
            }
            //cout << dp[3][7] << endl;
            /*REP(i,N){
                cout << flag[i] << endl;
            }*/
            /*int maxi = 0;
            REP(i,N-1){
                FOR(k,i,N){
                    if(dp[i][k]){
                        maxi = max(maxi,k-i);
                    }
                }
            }*/
            /*int ans_now = 0;
            int now = 0;
            while(now < N-1){
                while(!flag[now]){
                    now++;
                }
                for(int next = N; now < next; next--){ //バグりポイント flag[i] = trueだけの情報では、それが前に繋がっているのか後ろに繋がっているかわからないから、nextの範囲は指定しないといけない。
                    if(dp[now][next]){
                        ans_now += (next-now);
                        now = next;
                    }
                }
            }*/
            ans.push_back(dp[0][N]); 
            
        }
    }
}

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

int main() {
  unsigned int N = 0;
  vector<int> num;
  int w = 0;

  cin >> N;
  while(N != 0) {
    for(unsigned int i = 0; i < N; i++) {
      cin >> w;
      num.push_back(w);
    }

    while(true) {
      unsigned int i;
      for(i = num.size()-1; i > 0; i--)
	if(num.at(i)-num.at(i-1) >= -1 && num.at(i)-num.at(i-1) <= 1)
	  break;

      if(i == 0)
	break;
      num.erase(num.begin()+i);
      num.erase(num.begin()+i-1);
      
      if(num.empty())
	break;
    }

    cout << N-num.size() << endl;

    num.clear();

    cin >> N;
  }


  return 0;
}

C++: #include<iostream>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

//#define MAX 300
int n;
int* w;
vector<pair<int*,int>> wmap;

int* pop(int* w,int a) { //w???a????????¨a+1?????????????????????
	int *t;
	t = new int[n];
	for (int i = 0; i < n; i++) {
		if(i < a) t[i] = w[i];
		else t[i] = w[i + 2];
	}
	t[n - 2] = -1;
	t[n - 1] = -1;
	return t;
}


int daruma(int* w) {
	for (int i = 0; i < wmap.size(); i++) {
		if (memcmp(w, wmap[i].first, sizeof(w)) == 0) {
			return wmap[i].second;
		}
	}

	int max = 0;
	for (int i = 0; i < n - 1; i++) {
		if (w[i] != -1 && w[i + 1] != -1 && abs(w[i]-w[i+1])<=1) {
			int tmp = daruma(pop(w, i))+2;
			if (tmp > max) max = tmp;
		}
	}

	wmap.push_back(make_pair(w,max));
	return max;
}


int main() {
	cin >> n;
	while (n) {
		w = new int[n]();
		for (int i = 0; i < n; i++) {
			cin >> w[i];
		}
		cout << daruma(w) << endl;


		cin >> n;
	}


	return 0;
}