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<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
int main() {
	int n;
	while (cin >> n, n) {
		int dp[300][300];
		int w[300];
		rep(i, n) {
			cin >> w[i];
		}
		rep(i, n) {
			dp[i][i] = 0;
			if (i < n - 1) {
				dp[i][i + 1] = 0;
				if (abs(w[i] - w[i + 1]) <= 1) {
					dp[i][i + 1] = 2;
				}
			}
		}
		Rep(l, 2,n) {
			rep(i, n - l) {
				int res = 0;
				Rep1(j, i, i + l-1) {
					res = max(res, dp[i][j] + dp[j+1][i + l]);
				}
				res = max(res, dp[i + 1][i + l - 1]);
				if (abs(w[i] - w[i + l]) <= 1 && dp[i + 1][i + l - 1] == l - 1) {
					res = l + 1;
				}
				dp[i][i + l] = res;
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define X first
#define Y second

using namespace std;
typedef pair<int,int> pii;

int main(){
	int n, w[1004];
	while(cin >> n, n){

		rep(i,n) cin >> w[i];

		bool dp1[305][305]={};
		vector<pii> v;
		rep(i,n-1) if( abs(w[i] - w[i+1]) < 2 ){
			dp1[i][i+1] = true;
			v.emplace_back(i, i+1);
		}
		rep(k,n) rep(i,n){
			if( i+k >= n ) break;
			if( dp1[i][i+k] && 0 < i && i+k+1 < n ){
				if( abs(w[i-1] - w[i+k+1]) < 2 ){
					dp1[i-1][i+k+1] = true;
					v.emplace_back(i-1, i+k+1);
				}
			}
			rep(j,k+2){
				if( i+k+j >= n ) break;
				if( dp1[i][i+k] && dp1[i+k+1][i+k+j] ){
					dp1[i][i+k+j] = true;
					v.emplace_back(i, i+k+j);
				}
			}
		}
		//for(auto t: v) cout << t.X << "," << t.Y << " "; cout << endl;
		sort( v.begin(), v.end() );
		int dp[900005] = {}, pos=0;
		rep(i,n*n){
			while( pos < v.size() && v[pos].first == i ){
				int d  = v[pos].X;
				int nx = v[pos].Y + 1;
				//cout << d << "," << nx << endl;
				dp[nx] = max( dp[nx], dp[i] + nx - d );
				//cout << "i=" << i << " dp[" << nx << "] = " << dp[nx] << endl;
				pos++;
			}
			dp[i+1] = max( dp[i+1], dp[i] );
		}
		cout << dp[n*n] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using LL = long long;
using namespace std;
int main(){
    while(1){
        int N;cin >> N;
        if(N != 0){
            vector<int> vec;
            for(int a = 0;a < N;a++){
                int b;cin >> b;
                vec.push_back(b);
            }
            int dp[N+1][N+1];
            for(int a = 0;a <= N;a++){
                for(int b = 0;b <= N;b++){
                    dp[a][b] = -1;
                }
            }
            std::function<int(int,int)> calc = [&](int l, int r){
                if(dp[l][r] != -1)return dp[l][r];

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

                int ins = 0;

                if(abs(vec.at(l) - vec.at(r-1)) <= 1 && calc(l+1,r-1) == r - l -2){
                    ins = r - 1;
                }
                for(int mid = l + 1 ;mid <= r -1 ;mid++){
                    ins = max(ins, calc(l,mid) + calc(mid, r));
                }
                return dp[l][r] = ins;
            };
            calc(0,N);
            cout<<dp[0][N]<<endl;
        }else{
            return 0;
        }
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf =  1e9;


int main(){
 while(1){
  ll n;
  cin>>n;
  if(n==0)break;
  vector<ll> w(n);
  for(ll i=0;i<n;i++){
   cin>>w[i];
  }
  vector<vector<bool> > take( n , vector<bool>(n,false) );
  for(ll i=0;i+1<n;i++){
   if( abs(w[i]-w[i+1]) <= 1 ) take[i][i+1] = true;
  }
  
  for(ll l=3;l<n;l+=2){
   for(ll i=0;i+l<n;i++){
    if( take[i+1][i+l-1] && abs(w[i]-w[i+l])<=1 ){
     take[i][i+l] = true;
    }
     
     for(ll j=i ; j+1<i+l;j++ ){
      if( take[i][j] && take[j+1][i+l] ){
       take[i][i+l] = true;
       break;
      }
     }


   }
  }
/*
  for(ll i=0;i<n;i++){
   for(ll j=i+1;j<n;j++)if(take[i][j])cout<<1<<' ';
   else cout<<0<<' ';
   cout<<endl;
  }
*/
  vector<ll> dp(n,0);
  for(ll i=0;i<n;i++){
   if( take[0][i] ) dp[i] = i+1;
  }
  for(ll i=1;i<n;i++){
   dp[i] = max(dp[i],dp[i-1]);
   for(ll j=i;j>=1;j--){
    if( take[j][i] ) dp[i] = max( dp[i] , dp[j-1] + i-j+1 );
   }
  }
  cout<<dp[n-1]<<endl;

 }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
using namespace std;
using ll = long long;
const int inf = 1e8;

int dp[305][305];

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        vector<int> w(n);
        rep(i,n) cin >> w[i];
        for (int i = n-1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (j < i) continue;
                else if (j == i) dp[i][j] = 0;
                else if (j == i+1) {
                    if (abs(w[i]-w[j]) <= 1) dp[i][j] = 2;
                    else dp[i][j] = 0;
                }
                else {
                    if (abs(w[i]-w[j]) <= 1 && dp[i+1][j-1] == j-i-1) dp[i][j] = j-i+1;
                    for (int k = i; k <= j-1; k++) {
                        dp[i][j] = max(dp[i][k]+dp[k+1][j],dp[i][j]);
                    }
                }
            }
        }
        cout << dp[0][n-1] << endl;
        rep(i,n)rep(j,n) dp[i][j] = 0;
    }
}