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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>
using namespace std;

vector<int> a;         //命令 a[1]-a[n] 1-n ->0-9,u
vector<vector<int> > ok;
//vector<vector<int> > doteki;
int n;
int sec(int p,int q);
void pr();

int main(){
  scanf ("%d",&n);
  while(n){
    a.assign(n,0);
    ok.assign(n,a);
    //doteki.assign(n,a);
    for(int t=0;t<n;t++){
      scanf ("%d",&a[t]);
    }
    for(int s=0;(s+1)<n;++s){
      ok[s][s+1]=sec(s,s+1);
    }
    for(int t=3;t<n;t=t+2){
      for(int s=0;(s+t)<n;++s){
        if((ok[s][s+1]&&ok[s+2][s+t])||(sec(s,s+t)&&ok[s+1][s+t-1])||(ok[s][s+t-2]&&ok[s+t-1][s+t])){
          ok[s][s+t]=t+1;
        }
      }
    }
    
    
    for(int x=1;x<n;++x){
      for(int y=0;y+x<n;++y){
        for(int z=y;z<y+x;++z){
          ok[y][y+x]=max(ok[y][y+x],ok[y][z]+ok[z+1][y+x]);
        }
        //pr();
      }
    }
    printf("%d\n",ok[0][n-1]);
    scanf ("%d",&n);
  }
}



//変化したところだけ見る



int sec(int p,int q){
  if(-1<=(a[p]-a[q])&&(a[p]-a[q])<=1){
    return 2;
    //ok[p][q]=1;
  }
  else{
    return 0;
    //ok[p][q]=0;
  }
}

void pr(){
  for(int x=0;x<n;++x){
    for(int y=0;y<n-1;++y){
      printf("%d ",ok[x][y]);
    }
    printf("%d\n",ok[x][n-1]);
  }
  printf("\n");
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(int i=(a),i##_cond=(b);i<i##_cond;++i)

int main(){
  int n;
  while(cin >> n, n){
    vector<int> w(n); rep(i,n) cin >> w[i];
    vector<vector<int>> dp(n+1,vector<int>(n+1));

    // 区間DP
    FOR(j,2,n+1){ // 区間長
      for(int i = 0; i+j <= n; ++i;){ // 区間左端 dp[i][i+j]を決定する
	//printf("[%d,%d)\n",i,i+j);
	FOR(k,2,j-1) // dp[i][i+k],dp[i+k][i+j]を元に計算
	  dp[i][i+j] = max(dp[i][i+j], dp[i][i+k]+dp[i+k][i+j]);
	
	if(abs(w[i] - w[i+j-1]) <= 1)
	  dp[i][i+j] = max(dp[i][i+j], dp[i+1][i+j-1] + 2);
      }
    }

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

C++: //区間dp
#include <bits/stdc++.h>
#define REP(i,n) for (long long i=0;i<(n);i++)
#define FOR(i,a,b) for (long long i=(a);i<(b);i++)
#define RREP(i,n) for(long long i=n;i>=0;i--)
#define RFOR(i,a,b) for(long long i=(a);i>(b);i--)
#define dump1d_arr(array) REP(i,array.size()) cerr << #array << "[" << (i) << "] ==> " << (array[i]) << endl
#define dump2d_arr(array) REP(i,array.size()) REP(j,array[i].size()) cerr << #array << "[" << (i) << "]" << "[" << (j) << "] ==> " << (array[i][j]) << endl
#define dump(x)  cerr << #x << " => " << (x) << endl
#define dumpP(p) cerr << "( " << p.first << " , " << p.second << " )" << ends
#define SORT(c) sort((c).begin(),(c).end())
#define MIN(vec) *min_element(vec.begin(), vec.end())
#define MAX(vec) *max_element(vec.begin(), vec.end())
#define UNIQ(vec) vec.erase(unique(vec.begin(), vec.end()),vec.end()) //ソートの必要あり
#define IN(n,m)  (!(m.find(n) == m.end()))
#define ENUM(m) for (auto itr = m.begin(); itr != m.end(); ++itr)
#define dump_MAP(m) for(auto itr = m.begin(); itr != m.end(); ++itr) { cerr << itr->first << " --> "  << itr->second << endl; }
#define FINDL(vec,x) (lower_bound(vec.begin(),vec.end(),x) - vec.begin())
#define FINDU(vec,x) (upper_bound(vec.begin(),vec.end(),x) - vec.begin())
#define ROUND(N) setprecision(N)
#define ROUND_PRINT(N,val) cout << fixed;cout << setprecision(N) << val << endl
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define INARR(h,w,x,y) (0 <= y && y < h && 0 <= x && x < w)
#define EQ(a,b) (abs(a - b) < 1e-10)
using namespace std;
constexpr int dx[4] = {0,1,0,-1};
constexpr int dy[4] = {1,0,-1,0};
constexpr long double pi = M_PI;
constexpr double eps = 1e-10;
constexpr long mod = 1000000007;
constexpr short shINF = 32767;
constexpr long loINF = 2147483647;
constexpr long long llINF = 9223372036854775807;
typedef long long LL;
typedef vector<long> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<LL,LL> pr;
typedef vector<bool> VB;
typedef vector<VB>  VVB;
typedef vector<pr> VP;
struct Order {
	bool operator() (pr const& a,pr const& b) const {
		return a.first > b.first || ((a.first == b.first) && (a.second > b.second));
	}
};

VVI dp;
VI w;
int n;

int dfs(int l,int r) {
	if (dp[l][r] != loINF) return dp[l][r];
	int width = (r - l) + 1;
	if (width == 2) {
		if (abs(w[l] - w[r]) <= 1) return 2; else return 0;
	}
	if (width == 4) {
		int ans = dfs(l,l+1)+dfs(l+2,r);
		int ans2 = dfs(l+1,l+2);
		if (ans2 == 2 && abs(w[l]-w[r]) <= 1) ans2 += 2;
		return dp[l][r] = max(ans,ans2);
	}

	int ans = 0;
	int ans2 = dfs(l+1,r-1);
	if (ans2 == width-2 && abs(w[l]-w[r]) <= 1) ans2 += 2;
	else {
		for (int i = (l+1);i <= (r-2);i+=2) {
			ans = max(ans,dfs(l,i)+dfs(i+1,r));
			if (ans == width) break;
		}
	}
	return dp[l][r] = max(ans,ans2);
}

int main(void) {
	while (1) {
		cin >> n;
		if (n == 0) break;
		w.resize(n);
		dp = VVI(n,VI(n,loINF));
		REP(i,n) { cin >> w[i];}
		if (n % 2) cout << max(dfs(1,n-1),dfs(0,n-2)) << endl;
		else cout<< dfs(0,n-1) << endl;
		//dump2d_arr(dp);
	}
}

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

int dp[305][305], w[305];

int rec(int l, int r){
    if(r-l==1) return 0;
    if(r-l==2){
        if(abs(w[r-1]-w[l])<=1) return 2;
        else return 0;
    }

    if(dp[l][r]>-1) return dp[l][r];
    if(abs(w[r-1]-w[l])<=1 && rec(l+1, r-1)==r-l-2) return dp[l][r] = r - l;

    int res = 0;
    for(int i=l;i<r-1;i++){
        res = max(res, rec(l, i+1)+rec(i+1, r));
    }
    return dp[l][r] = res;
}

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    while(true){
        int N;cin>>N;
        if(!N)return 0;
        for(int i=0;i<N;i++)cin>>w[i];
        for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) dp[i][j] = -1;
        printf("%d\n", rec(0, N));
    }
}