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 <bits/stdc++.h>
using namespace std;

int n;
vector<int> w;
vector<vector<int>> dp;

int solve(int l, int r);

int main() {
  while(1) {
    cin >> n;
    if(n == 0) break;
    w.resize(n);
    for(int i = 0; i < n; ++i) cin >> w[i];
    dp.assign(n, vector<int>(n, -1));
    for(int i = 0; i < n; ++i) dp[i][i] = 0;
    for(int i = 0; i < n - 1; ++i)
      if(abs(w[i + 1] - w[i]) <= 1) dp[i][i + 1] = 2;
    cout << solve(0, n - 1) << endl;
  }
  return 0;
}

int solve(int l, int r) {
  if(dp[l][r] != -1) return dp[l][r];
  int ans = 0;
  for(int i = l; i < r; ++i)
    ans = max(ans, solve(l, i) + solve(i + 1, r));
  if(r - l + 1 >= 4 && solve(l + 1, r - 1) == r - l - 1 &&
     abs(w[l] - w[r]) <= 1)
    ans = r - l + 1;
  return dp[l][r] = ans;
}

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

int main() {
    int n;
    while(cin >> n, n) {
        vector<int> w(n);
        for(int i = 0; i < n; ++i) {
            cin >> w[i];
        }
        vector<vector<int>> dp(n, vector<int>(n, -1));
        function<int(int, int)> solve = [&] (int l, int r) {
            if(l >= r) return 0;
            int& res = dp[l][r];
            if(res != -1) return res;
            for(int i = l; i < r; ++i) {
                res = max(res, solve(l, i) + solve(i + 1, r));
            }
            if(abs(w[l] - w[r]) <= 1) {
                if(solve(l + 1, r - 1) == max(0, r - l - 1)) {
                    res = r - l + 1;
                }
            }
            return res;
        };
        cout << solve(0, n - 1) << endl;
    }
}

C++: #include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1611&lang=jp
// references : http://ferin-tech.hatenablog.com/entry/2017/05/17/123005
int main(void) {
	cin.tie(0); ios::sync_with_stdio(false);
	int n;
	while (cin >> n, n) {
		vector<int> w(n);
		for (int i = 0; i < n;i++)cin >> w[i];
		vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));

		for (int W = 2; W <= n; W++) {
			for (int l = 0; l < n;l++) {
				int r = l + W - 1;
				if (r >= n) continue;

				/* 
				   l+1         r-1
				   [] [] [] [] [] = W - 2 ( = (r - l - 1) - 2)
				l                  r
				{} [] [] [] [] [] {}      w[r] - w[l] <= 1  ==>   dp[l][r] = W
				*/
				if (dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r]) <= 1) dp[l][r] = W;


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

C++: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;

#define MAX 310

int n,w[MAX];
int memo[MAX][MAX];

bool dfs(int L,int R) { // memo??????????????¨??§



  if( R - L <= 1 ) {
    return memo[L][R] = 0;
  }
  if( R - L <= 2 ) {
    assert( R-1 < n);
    if( abs(w[R-1] - w[L]) <= 1 ) {
      memo[L][R] = 1;
    } else {
      memo[L][R] = 0;
    }
    return memo[L][R];
  }
  if( memo[L][R] != -1 ) return memo[L][R];
  int &ret = memo[L][R];
  ret = 0;
  for(int med=L;med<R;++med) {
    if( dfs(L,med+1) && dfs(med+1,R) ) {
      ret = 1;
      break;
    }
  }



  if( ret == 0 ) {
    if( abs(w[L]-w[R-1] ) <= 1 )  {
      if( dfs(L+1,R-1) ) {
	ret = 1;
      }
    }
  }
 Fin:;
  return ret;
}

int dp[MAX];

void compute() {
  memset(memo,-1,sizeof memo);
  rep(i,n) memo[i][i+1] = 0;
  REP(len,2,n+1) {
    for(int L=0;L<n;++L) {
      int R = L + len;
      if( R > n ) break;
      dfs(L,R);
    }
  }
  //rep(L,n) for(int R=L+1;R<=n;++R) dfs(L,R); // [0,n)
  /*
  rep(L,n) for(int R=L+1;R<=n;++R) {
    cout << "[" << L << "," << R << ") = " << memo[L][R]  << endl;
  }
  */

  memset(dp,0,sizeof dp);
  int maxi = 0;
  rep(i,n) {
    dp[i] = max(dp[i],dp[i-1]);
    REP(j,i+1,n+1) {
      int cost = -1;
      if( memo[i][j] ) {
	cost = ( j - i)  ;
      }
      if( cost == -1 ) continue;
      if( dp[j] < dp[i] + cost ) {
	dp[j] = dp[i] + cost;
	maxi = max(maxi,dp[j]);
      }
    }
  }
  cout << maxi << endl;
}

int main(){
  while( cin >> n, n ) {
    rep(i,n) cin >> w[i];
    compute();
  }
  return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
 
#define rep(i, a) for (int i = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int i = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int i = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define RS resize
#define CINV(v, N) do {\
	v.RS(N);\
	rep(i, N) cin >> v[i];\
} while (0);
#define RCINV(v, N) do {\
	v.RS(N);\
	rrep(i, N) cin >> v[i];\
} while (0);
 
#define MOD 1000000007
bool init();
int solve();

signed main()
{
    while (init()) {
    	cout << solve() << endl;
	}
}

int N;
vector<int> w;

bool init()
{
	cin >> N;
	if (N == 0) return false;
	w.RS(N);
	rep(i, N) cin >> w[i];
	return true;
}


int solve()
{
	vector<vector<int> > dp;
	dp.RS(N, vector<int>(N, 0));
	rep(i, N) rep(j, N) dp[i][j] = abs(j - i) + 1;
	for (int i = 0; i + 1 < N; i++) {
		if (abs(w[i] - w[i+1]) <= 1) dp[i][i+1] = 0;
	}

	for (int l = 3; l <= N; l++) {
		for (int i = 0; i + l - 1 < N; i++) {
			int j = i + l - 1;
			int r = dp[i][j-2] + dp[j-1][j];
			r = min(r, dp[i][i+1] + dp[i+2][j]);
			if (abs(w[i] - w[j]) <= 1 && dp[i+1][j-1] == 0) {
				r = 0;
			}
			dp[i][j] = r;
		}
	}
	return N - dp[0][N-1];
}