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++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>  
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 10000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i,n) for(long long i = 0;i < n;++i)
int main() {
	while (true) {
		int n;
		scanf("%d", &n);
		if (n == 0) return 0;
		vector<int> input;
		REP(i, n) {
			int a;
			scanf("%d", &a);
			input.push_back(a);
		}
		int ok[300][300] = {};
		vector<pair<int, int>> next;
		for (int i = 1; i < n; ++i) {
			if (abs(input[i] - input[i - 1]) <= 1) {
				ok[i - 1][i] = 1;
				next.push_back(make_pair(i - 1, i));
			}
		}
		for (int i = 0; i < next.size(); ++i) {
			int left = next[i].first;
			int right = next[i].second;
			if (left != 0 && right != n - 1) {
				//ok to expand
				if (abs(input[left - 1] - input[right + 1]) <= 1) {
					//ok
					if (ok[left - 1][right + 1] == 0) {
						ok[left - 1][right + 1] = 1;
						next.push_back(make_pair(left - 1, right + 1));
					}
				}
			}
			for (int i = 0; i < left; ++i) {
				if (ok[i][left - 1] == 1 && ok[i][right] == 0) {
					ok[i][right] = 1;
					next.push_back(make_pair(i, right));
				}
			}
			for (int i = right + 2; i < n; ++i) {
				if (ok[left][i] == 0 && ok[right+1][i] == 1) {
					ok[left][i] = 1;
					next.push_back(make_pair(left, i));
				}
			}
		}
		int dp[500] = {};
		for (int i = 0; i < n; ++i) {
			dp[i + 1] = max(dp[i], dp[i + 1]);
			for (int q = i + 1; q < n; ++q) {
				if (ok[i][q] == 1) {
					dp[q + 1] = max(dp[q + 1], dp[i] + (q - i + 1));
				}
			}
		}
		cout << dp[n] << endl;
	}
}

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

int main(){
  int n;
  while(cin >> n, n){
    int w[n];
    for(int i = 0; i < n; ++i)
      cin >> w[i];
    
    int dp[n][n];
    for(int i = 0; i < n; ++i){
      fill(dp[i],dp[i]+n,0);
    }
    
    for(int i = 0; i < n-1; ++i){
      for(int j = 0; j < n; ++j){
	if(j+i+1 > n-1) break;
	if(dp[j+1][j+i] == i && abs(w[j]-w[j+i+1]) < 2){
	  dp[j][j+i+1] = dp[j+1][j+i] + 2;
	}else{
	  for(int k = 0; k < i; ++k){
	    dp[j][j+i+1] = max(dp[j][j+i+1],dp[j][j+k]+dp[j+k+1][j+i+1]);
	  }
	}
      }
    }
    /*
    for(int i = 0; i < n; ++i){
      for(int j = 0; j < n; ++j){
      cout << dp[i][j] << " ";
      }
      cout << endl;
    }
    */
    cout << dp[0][n-1] << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE>
void print_vec(const vector<TYPE>& v){
	for(int i=0; i<v.size(); i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
  cout << endl; cout << "       ";
  for(int i=0; i<v[0].size(); i++) cout << i << "   "; 
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}
 
int chmax(int &a, int b) {return a = max(a, b);}

int solve(int n){
	vector<int> block(n);
	for(int i=0; i<n; i++) cin >> block[i];

	//dp[l][r]: 区間[l, r]考えた時，取り除けるブロックの最大個数
	//http://kutimoti.hatenablog.com/entry/2018/03/10/220819
	vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
	//初期化は0でよい


	for(int w=2; w<=n; w++){
		for(int l=0; l+w<=n; l++){
			int r = l+w;
			if(dp[l+1][r-1]==w-2 && abs(block[l]-block[r-1])<=1) dp[l][r] = w;
			for(int mid=l; mid<r; mid++){
				chmax(dp[l][r], dp[l][mid]+dp[mid][r]);
			}
		}
	}
	// cout << " block: "; print_vec(block);
	// print_vec2(dp);
	return dp[0][n];
}

int main(){
	
	vector<int> ans;
	while(true){
		int n;
		cin >> n;
		if(n == 0) break;
		ans.push_back(solve(n));
	}
	for(int i=0; i<ans.size(); i++) cout << ans[i] << endl;


	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

vector<int>as;
bool ok(int a,int b){
  return abs(as[a]-as[b])<=1;
}

int main() {
  while(1){
    
    int N;cin>>N;
    if(!N)break;
    as.resize(N);
    vector<vector<int>>dp(N+1,vector<int>(N+1,-100000));
    for(int i=0;i<N;++i){
      int a;cin>>a;as[i]=a;
    }
    for(int i=0;i<=N;++i){
      dp[i][i]=0;
    }
    for(int t=2;t<=N;++t){
      for(int l=0;l<=N-t;++l){
	const int r=l+t;
	dp[l][r]=max(dp[l+1][r],dp[l][r-1]);
	if(ok(l,l+1))dp[l][r]=max(dp[l+2][r]+2,dp[l][r]);
	if(ok(r-2,r-1))dp[l][r]=max(dp[l][r-2]+2,dp[l][r]);
	if(ok(l,r-1)){
	  if(dp[l+1][r-1]==(r-l-2)){
	    dp[l][r]=max(dp[l+1][r-1]+2,dp[l][r]);
	  }
	}
      } 
    }

    int ans=dp[0][N];
    ans=max(0,ans)
    cout<<ans<<endl;
  }
  return 0;
}