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;

#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int,int> Interval;

template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {
    _for(i,0,v.size()) os << v[i] << " ";
    return os;
}

template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){
    for(typename set<T>::iterator it = v.begin();it != v.end();it++)
    os << *it <<" ";
    return os;
}

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& v){
    os << v.first <<" "<<v.second<<endl;
    return os;
}

const int maxn = 300+5;
int mem[maxn];
int d[maxn][maxn];
vector<int> A[maxn];
int n;

int dp(int l,int r){
    int &ans = d[l][r];
    if(ans >= 0) return ans;
    if(r-l <= 0){
        ans = 0;
        return ans;
    }

    ans = dp(l+1,r);
    for(auto a:A[l]){
        if(a > r) break;
        int dd = dp(l+1,a-1);
        int nex = dp(a+1,r);
        if(dd == a-l-1) nex = dd + nex + 2;
        else nex = nex + dd;
        ans = max(ans,nex);
    }
    return ans;
}

int main()
{
    while(1)
    {
        scanf("%d", &n);
        memset(d,-1,sizeof(d));
        if(!n) break;
        memset(mem, 0, sizeof(mem));
        _for(i,0,maxn) A[i].clear();

        for(int i = 0; i < n; i++)
        {
            scanf("%d", &mem[i]);
        }

        for(int i = 0; i < n; i++){
          for(int j = i+1; j < n; j++)
          {
              if(abs(mem[i]- mem[j])<=1)
              {
                  A[i].push_back(j);
              }
          }
        }
        cout << dp(0,n-1) << endl;

    }

}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int n, a[1010];
int dp[1010][1010], dp2[1010] = {};

int ok(int l, int r){
    int &res = dp[l][r];
    if(res != -1) return res;
    res = false;
    if(l+0 == r) res = true;
    for(int i = l+2; i < r; i += 2) if(ok(l,i) && ok(i,r)) res = true;
    if(abs(a[l] - a[r-1]) <= 1 && ok(l+1, r-1)) res = true;
    return res;
}

int main(){
    while(cin >> n && n){
        rep(i, n) cin >> a[i];
        memset(dp, -1, sizeof dp);
        memset(dp2, 0, sizeof dp2);
        dp2[0] = 0;
        rep(i, n){
            dp2[i+1] = max(dp2[i+1], dp2[i]);
            for(int j = i + 2; j <= n; j += 2){
                if(ok(i, j)) dp2[j] = max(dp2[j], dp2[i] + j - i);
            }
        }
        cout << dp2[n] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,w[300];
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>w[i];
    int dp[300][300]={};
    int ans=0;
    for(int i=1;i<n;i++)dp[i][i-1]=0;
    for(int i=1;i<n;i++)
      for(int j=0;j+i<n;j++){
	for(int k=j;k<j+i;k++)
	  dp[j][j+i]=max(dp[j][j+i],dp[j][k]+dp[k+1][j+i]);
	if(dp[j+1][j+i-1]==i-1&&abs(w[j]-w[j+i])<2)dp[j][j+i]=i+1;
      }
    cout<<dp[0][n-1]<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int n, w[300];
int can[301][301], dp[301];

int rec(int l, int r){
	int &res = can[l][r];
	if(res >= 0) return res;
	if(l == r) return res = 1;
	if(l >= r - 1 || (r - l) % 2) return res = 0;
	
	if(abs(w[l] - w[r - 1]) < 2 && rec(l + 1, r - 1)) return res = 1;
	for(int i = l + 2; i < r; i += 2) if(rec(l, i) && rec(i, r)) return res = 1;
	return res = 0;
}

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	while(cin >> n, n){
		memset(can, -1, sizeof(can));
		memset(dp, 0, sizeof(dp));
		
		rep(i, n) cin >> w[i];
		rep(i, n){
			dp[i + 1] = max(dp[i + 1], dp[i]);
			for(int j = i - 1; j >= 0; j -= 2) if(rec(j, i + 1))
				dp[i + 1] = max(dp[i + 1], dp[j] + i - j + 1);
		}
		cout << dp[n] << endl;
	}
	return 0;
}