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<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

const int MAX = 710;
const int MOD = 1000000007;

ll dp[MAX][MAX];
// dp[i][j] is from i to j no max can remove

int main(){
  vector<ll> ans;
  while(1){
    ll N; cin >> N; if(N == 0) break;
    vector<ll> a(N); rep(i,N) cin >> a[i];
    // dp syokika
    rep(i,MAX) rep(j,MAX) dp[i][j] = 0;

    repn(i,N){
      // i は何個先か
      rep(j,N){
        // j is start
        if(dp[j + 1][j + i - 1] == (i - 1) and abs(a[j] - a[j + i]) < 2){
          // 両端に追加できる
          dp[j][j + i] = max(dp[j][j + i] , dp[j + 1][j + i - 1] + 2);
        }
        for(ll k = j; k <= i + j; k++){
          // kを飛ばす
          dp[j][j + i] = max(dp[j][j + i] , dp[j][k] + dp[k + 1][j + i]);
        }
      }
    }
    ans.pb(dp[0][N - 1]);
  }

  rep(i,ans.size()) cout << ans[i] << endl;
}

C++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int dp1[310][310], dp2[310];
int solve_testcase() {
    int N; scanf("%d", &N);
    if(N == 0) return 1;
    vector<int> A(N);
    for(int i=0; i<N; i++) scanf("%d", &A[i]);
    
    fill(dp1[0], dp1[N+1], false);
    for(int i=0; i<N; i++) {
        dp1[i][i] = true;
    }
    for(int d=2; d<=N; d++) {
        for(int l=0; l<N-d+1; l++) {
            int r = l + d;
            if(abs(A[l] - A[r-1]) <= 1 and dp1[l+1][r-1]) dp1[l][r] = true;

            if(r - l >= 4) {
                for(int k=l+2; k<=r-2; k++) {
                    if(dp1[l][k] and dp1[k][r]) dp1[l][r] = true;
                }
            }
        }
    }

    fill(dp2, dp2 + N + 1, -1);
    dp2[0] = 0;
    for(int i=0; i<N; i++) {
        chmax(dp2[i+1], dp2[i]);
        for(int j=i+1; j<=N; j++) {
            if(!dp1[i][j]) continue;
            chmax(dp2[j], dp2[i] + (j - i));
        }
    }
    printf("%d\n", dp2[N]);
    return 0;
}

int main() {
    while(!solve_testcase());
    return 0;
}