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++: #ifdef DEBUG
#include "../utils.cpp"
#endif
#include <bits/stdc++.h>
/*
Problem


Category
DP
 
URL:


考察:



Date: 2020/06/20
// 1926
// 1935
// 1950

*/
 

using namespace std;
using ll = int64_t;
#define int int64_t
#define INF 10e9

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int N;
vector<int> W;
vector<vector<int>> dp;
vector<vector<int>> memo;

/* ======================== */


void getInput() {
    cin>>N;
    if (N==0) {
        exit(0);
    }

    W = vector<int>(N);
    memo = vector<vector<int>>(N, vector<int>(N, INF));
    for(int i=0; i<N; ++i) {
        cin>>W[i];
    }
}

int getMemo(int s, int t) {
    assert(s >=0 and t>= 0 and s<N and t<N);
    if (memo[s][t] != INF) {
        return memo[s][t];
    } else {
        if (t - s == 0) {
            memo[s][t] = 1;
        } else if (t-s == 1) {
            if (abs(W[t] - W[s]) <= 1) {
                memo[s][t] = 0;    
            } else {
                memo[s][t] = 2;
            }
        } else if (t - s >= 2) {
            int score = getMemo(s, t-1) + 1;

            for(int i=t-1; i >= s; --i) {
                if (abs(W[t] - W[i]) > 1) continue;

                if (t-i==1 or getMemo(i+1, t-1) == 0) {
                    if (i-1 < s) {
                        score = 0;
                    }
                    else {
                        score = min(score, getMemo(s, i-1));
                    }
                }
            }
            memo[s][t] = score;
        }
    }
    return memo[s][t];
}
 

void solve() {
    cout<<N - getMemo(0, N-1)<<endl;
}
 
 
signed main(){
    while(1) {
        getInput();
        solve();
    }
    
    return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n;
	cin >> n;
	vi num(n);
	REP(i, n)
		cin >> num[i];
	vector<vector<bool>> able(n, vb(n, false));
	for (int i = 2;i < n + 1;i += 2) {
		for (int j = 0;j + i - 1 < n;j++) {
			if (i == 2) {
				if (abs(num[j] - num[j + i - 1]) <= 1)
					able[j][j + i - 1] = true;
			}
			else {
				if (abs(num[j] - num[j + i - 1]) <= 1 && able[j + 1][j + i - 2])
					able[j][j + i - 1] = true;
				REP(k, i / 2 - 1)
					if (able[j][j + k * 2 + 1] && able[j + k * 2+2][j + i - 1])
						able[j][j + i - 1] = true;
			}
		}
	}
	vvi DP(n, vi(n, 0));
	REP(i, n)
		REP(j, n)
		if (able[i][j])
			DP[i][j] = j - i + 1;
	for (int i = 3;i < n + 1;i++) {
		for(int j=0;j+i-1<n;j++)
			REP(k,i-2)
			DP[j][j + i - 1] = max(DP[j][j + i - 1], DP[j][j+1+k]+ DP[j+2+k][j + i - 1]);
	}
	cout << DP[0][n - 1] << endl;
}

C++: #include <bits/stdc++.h>
 
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
 
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
 
//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
 
 
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
 
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-10;
 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)


//ここから編集
int dp[310][310];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n; 
  while(cin >> n, n){
    vector<ll> w(n);
    REP(i,n) cin >> w[i];

    vector<vector<bool>> ok(n+1, vector<bool>(n+1, false)); //開区間 [i~j)消せますよの意
    
    vector<vector<int>> dp(n+1, vector<int>(n+1, 0));


    for(int i=2; i<=n; i++){
      for(int j=0; j<n; j++){
        if(j+i > n) break;
        
        if(dp[j+1][j+i-1] == i-2&& abs(w[j] - w[j+i-1]) <= 1) dp[j][j+i] = i;

        for(int k=j+1; k<i+j; k++){
          dp[j][i+j] = max(dp[j][i+j], dp[j][k] + dp[k][i+j]);
        }
      }
    }
    
    cout << dp[0][n] << endl;
  } 
  return 0;
}