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;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
//typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int dp[300][300];

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (true) {
    int N; cin >> N;
    if (not N) break;
    vector<int> W(N); REP(i, N) cin >> W[i];
    memset(dp, 0, sizeof(dp));
    FOR(w, 2, N + 1) REP(left, N - w + 1) {
      // [left, right]
      int right = left + w - 1;
      if (dp[left + 1][right - 1] == w - 2 and abs(W[left] - W[right]) <= 1) {
        dp[left][right] = w;
      } else {
        FOR(mid, left + 1, right) {
          dp[left][right] = max(dp[left][right], dp[left][mid] + dp[mid + 1][right]);
        }
      }
    }
    cout << dp[0][N - 1] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 305;
int dp[MAX][MAX];
int num[MAX];

int dfs(int l, int r){
    if(dp[l][r]!=-1)return dp[l][r];
    if(l >= r){
        dp[l][r]=0;
        return 0;
    }
    if(l+1==r){
        if(abs(num[l]-num[r])<2){
            dp[l][r] = 2;
            return 2;
        }
        else{
            dp[l][r] = 0;
            return 0;
        }
    }
    for(int p = l; p <= r; p++){
        dp[l][r] = max(dp[l][r], dfs(l, p) + dfs(p+1, r));
    }
    if(abs(num[l]-num[r])<2 && dfs(l+1, r-1)==(r-l-1)){
        dp[l][r]=r-l+1;
    }
    return dp[l][r];
}

int main(){
    for(int n;cin>>n,n;){
        for(int i = 0; i < MAX; i++){
            for(int j = 0; j<MAX;j++){
                dp[i][j]=-1;
            }
        }
        for(int i=1;i<=n;i++){
            cin>>num[i];
        }
        cout<<dfs(1, n)<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
const int INF = (int)1e9;
int N, W[300];
int dp[300][300];
// [i,j]
bool solve(int i, int j) {
  assert(0 <= i && i < N && 0 <= j && j < N);
  assert(i <= j);
  int &res = dp[i][j];
  if(res != -1) return res;
  res = false;
  if(i + 1 == j) if(abs(W[i] - W[j]) <= 1) res = true;
  if(i + 2 <= j) {
    if(abs(W[i] - W[j]) <= 1 && solve(i + 1, j - 1)) {
      res = true;
    }
  }
  for(int k = i; k < j; ++k) {
    if(solve(i, k) && solve(k + 1, j)) res = true;
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  while(true) {
    cin >> N;
    if(N == 0) break;
    REP(i,N) cin >> W[i];
    memset(dp, -1, sizeof(dp));
    REP(i,N) for(int j = i + 1; j < N; ++j) {
      solve(i, j);
      // cerr << "[" << i << "," << j << "] = " << (solve(i, j) ? "yes" : "no") << endl;
    }
    vector<int> cost(N + 1, INF);
    cost[0] = 0;
    REP(i,N) {
      cost[i + 1] = min(cost[i + 1], cost[i] + 1);
      for(int j = N - 1; j > i; --j) {
        if(solve(i, j)) {
          cost[j + 1] = min(cost[j + 1], cost[i]);
        }
      }
    }
    // REP(i,N+1) {
    //   cerr << " " << cost[i];
    // }
    // cerr << endl;
    cout << N - cost[N] << endl;
  }
}

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

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

while(1){
  int n; cin >> n;
  if(n == 0) break;
  vector<int> w(n);
  for(int i = 0; i < n; i++){
    cin >> w[i];
  }

  vector< vector<int> > dp(301,vector<int> (301,-1));
  function< int(int,int) > rec =
  [&](int l,int r){
    if(dp[l][r] != -1) return dp[l][r];

    if(abs(l-r) <= 1) return 0LL;

    int res = 0;
    if(abs(w[l] - w[r-1]) <= 1 && rec(l+1,r-1) == (r-1)-(l+1)){
      res = r - l;
    }
    for(int mid = l+1; mid < r; mid++){
      res = max(res,rec(l,mid) + rec(mid,r));
    }
    return dp[l][r] = res;
  };
  cout << rec(0,n) << endl;
}
  return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    while(true) {
        int N;
        cin >> N;
        if(N == 0) break;
        vector<int> w(N);
        REP(i, N) cin >> w[i];
        vector< vector<int> > dp(N+5, vector<int>(N+5));

        int ans = 0;
        FOR(len, 1, N-1) {
            // if(len%2==0) continue;
            REP(i, N - len) {
                if(i + len >= N) continue; 
                if(abs(w[i] - w[i+len]) <= 1 && (dp[i+1][i+len-1] == len - 1))
                    dp[i][i+len] = max(dp[i][i+len], dp[i+1][i+len-1] + 2);
                FOR(j, i, i+len) {
                    // if((j-i)%2 == 1 && (i+len - (j+1)) % 2 == 1)
                        dp[i][i+len] = max(dp[i][i+len], dp[i][j] + dp[j+1][i+len]);
                }
            }
        }
        ans = dp[0][N-1];
        cout << ans << endl;
    }

    return 0;
}