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<vector>
#include<tuple>
#include<algorithm>
#include<cmath>
using namespace std;
vector<tuple<int, int, int>>vec; int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; vec.clear(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; } for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { vec.push_back(make_tuple(j - i, i, j)); } }
		sort(vec.begin(), vec.end());
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 0; i < vec.size(); i++) {
			int a1 = get<1>(vec[i]), a2 = get<2>(vec[i]);
			if (dp[a1][a2] == 0)continue;
			if (a2 < n - 2 && abs(a[a2 + 1] - a[a2 + 2]) <= 1)dp[a1][a2 + 2] = 1;
			if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)dp[a1 - 1][a2 + 1] = 1;
		}
		for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[i][j] == 1)dp[i][j] = j - i + 1; } }
		for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j; k < n; k++) { dp[i][k] = max(dp[i][k], dp[i][j - 1] + dp[j][k]); } } }
		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;
	while(cin >> n, n){

		int w[1004] = {};
		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,n) rep(i,n){
				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() );
		v.erase(unique(v.begin(), v.end()), v.end());

		int dp[900005] = {}, pos=0;
		rep(i,n) rep(k,n){
			if( i+k >= n ) break;
			if( dp1[i][i+k] ){
				dp[i+k+1] = max( dp[i+k+1], dp[i] + k+1 );
			}
			dp[i+1] = max( dp[i+1], dp[i] );
		}
//		rep(i,n) cout << dp[i] << " "; cout << endl;
		cout << dp[n] << endl;
//		rep(i,900002){
//			while( pos < v.size() && v[pos].first == i ){
//				int d  = v[pos].X;
//				int nx = v[pos].Y + 1;
//				dp[nx] = max( dp[nx], dp[i] + nx - d );
//				pos++;
//			}
//			dp[i+1] = max( dp[i+1], dp[i] );
//		}
//		cout << dp[900002] << endl;
	}
	return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second
#define mp make_pair
 
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1ll << 60;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, W[305], DP[305][305];

int DFS(int L, int R){
  if (DP[L][R] != -1) return DP[L][R];

  if (L + 1 == R) return DP[L][R] = 0;
  if (L + 2 == R){
    if (abs(W[L] - W[L + 1]) <= 1) return DP[L][R] = 2;
    else return DP[L][R] = 0;
  }

  int ans = 0;
  if (DFS(L + 1, R - 1) == (R - 1) - (L + 1)){
    ans = DFS(L + 1, R - 1);
    if (abs(W[L] - W[R - 1]) <= 1) ans += 2;
  }

  for (int i = L + 1; i < R; i++){
    ans = max(ans, DFS(L, i) + DFS(i, R));
  }
  return DP[L][R] = ans;
}

int Solve(){
  fill((int*)DP, (int*)(DP + N), -1);
  for (int i = 0; i < N; i++) cin >> W[i];
  cout << DFS(0, N) << endl;
  return 0;
}

int main(){
  while (cin >> N){
    if (N == 0) return 0;
    Solve();
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
#include <cmath>
#include <functional>

using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;

int n;

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


int rec(int l,int r){
	if(dp[l][r] != -1) return dp[l][r];

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

	int res = 0;
	//l [able] r ?
	if(abs(w[l] - w[r - 1]) <= 1 && rec(l + 1,r - 1) == r - l - 2)
	{
		res = r - l;
	}

	//[l any] [any r]
	for(int mid = l + 1;mid <= r - 1;mid++)
	{
		res = max(res , rec(l,mid) + rec(mid,r));
	}
	return dp[l][r] = res;
};
int main()
{
	while(cin >> n , n)
	{
		w.assign(n,0);
		for(int i = 0;i < n;i++) cin >> w[i];

		dp.assign(n + 1,vector<int>(n + 1,-1));


		//[l,r)

		cout << rec(0,n) << endl;
	}
}

C++: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for(int (i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;
template<class T, class U> inline bool chmax(T &a, const U &b) { if(a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if(a > b) { a = b; return true; } return false; }

int dp[310][310];

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  for(;;) {
    int n;
    cin >> n;
    if(n == 0) break;
    vector<int> w(n);
    rep(i,n) cin >> w[i];
    rep(i,310) rep(j,310) dp[i][j] = 0;
    REP(i,2,n+1) rep(l,n) {
      int r = l+i;
      if(r > n) continue;
      REP(j,l,r+1) {
        chmax(dp[l][r], dp[l][j] + dp[j][r]);
      }
      if(dp[l+1][r-1] == r-l-2 && abs(w[l]-w[r-1]) <= 1) {
        chmax(dp[l][r], r-l);
      }
      else chmax(dp[l][r], dp[l+1][r-1]);
    }
    cout << dp[0][n] << '\n';
  }
  return 0;
}