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;

int main() {
	vector<int> ress;
	while (1) {
		int n;
		cin >> n;

		if (n == 0) { break; }

		vector<long long> w(n + 1);
		for (int i = 1; i <= n; i++) {
			cin >> w[i];
		}

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

		int res = 0;
		for (int i = 1; i <= n - 1; i++) {
			if (abs(w[i] - w[i + 1]) <= 1) {
				dp[i][i + 1] = 1;
				res = 2;
			}
		}

		if (res == 0) { ress.push_back(0);  continue; }
		if (n <= 3) { ress.push_back(res); continue; }

		for (int i = 1; i <= n - 3; i++) {
			if ((dp[i + 1][i + 2] == 1 && abs(w[i] - w[i + 3]) <= 1)
				|| (dp[i][i + 1] == 1 && dp[i + 2][i + 3] == 1)
				) {
				dp[i][i + 3] = 1;
				res = 4;
			}
		}
//		if (res == 2) { ress.push_back(2);  continue; }
		if (n <= 5) { ress.push_back(res); continue; }

		for (int i = 5; i <= n - 1; i = i + 2) {
			for (int j = 1; j <= n; j++) {
				if (j + i > n) { continue; }

				bool okflg = false;

				for (int length = 2; length <= i - 1; length = length + 2) {

					if (dp[j][j +length-1] == 1 && dp[j + length][j + i] == 1) { 
						okflg = true; 
						break;
					}
				}

				if (okflg) { 
					dp[j][j + i] = 1;
					continue; 
				}

				if (abs(w[j] - w[j + i]) <= 1) {

					if (dp[j + 1][j + i - 1] == 1) { okflg = true; }
					else {
						for (int length = 2; length <= i - 3; length = length + 2) {
							if (dp[j + 1][j +1+ length - 1] == 1
								&& dp[j + 1+length][j + i - 1] == 1
								) {
								okflg = true;
								break;
							}
						}
					}
				}

				if (okflg == true) {
					dp[j][j + i] = 1;
				}
			}
		}

		res = 0;
		int i = 1;
		while (i < n) {
			for (int j = n; j >= i + 1; j--) {
				if (dp[i][j] == 1) {
					res += j - i + 1;
					i = j;
					break;
				}
			}
			i++;
		}
		int res2 = 0;
		i = n;
		while (i >1) {
			for (int j = 1; j <=n- 1; j++) {
				if (dp[j][i] == 1) {
					res2 += i-j + 1;
					i = j;
					break;
				}
			}
			i--;
		}

		res = max(res, res2);

		ress.push_back(res);
	}
	for (int i = 0; i < ress.size(); i++) {
		cout << ress[i] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#define INF 1000000000ll
#define MOD 1000000007ll
#define EPS 1e-10
#define REP(i,m) for(long long i=0; i<m; ++i)
#define FOR(i,n,m) for(long long i=n; i<m; ++i)
#define DUMP(n,a) for(long long dump=0; dump<n; ++dump) { cout<<a[dump]; if(dump!=n-1) cout<<" "; else cout<<endl; }
#define ALL(v) v.begin(),v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;

vector<vector<ll>> memo;
vector<ll> w;
ll dfs(ll l, ll r) {
	if(memo[l][r]!=INF) return memo[l][r];
	if(r-l+1<2) return memo[l][r]=0;
	if(r-l+1==2) {
		if(abs(w[l]-w[r])<=1) return memo[l][r]=2;
		else return memo[l][r]=0;
	}
	ll ret=0;
	if(dfs(l+1,r-1)==r-l-1) {
		if(abs(w[l]-w[r])<=1) return memo[l][r]=r-l+1;
		else ret=max(ret,r-l-1);
	}
	FOR(i,l,r) ret=max(ret,dfs(l,i)+dfs(i+1,r));
	return memo[l][r]=ret;
}
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(1) {
		ll n;
		cin>>n;
		if(n==0) break;
		w.resize(0);
		w.resize(n);
		REP(i,n) cin>>w[i];
		memo.resize(0);
		memo.resize(n);
		REP(i,n) {
			memo[i].resize(n);
			REP(j,n) memo[i][j]=INF;
		}
		cout<<dfs(0,n-1)<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n;
int dp[500][500];
vector<int> w;

// [l,r)
int rec(int l, int r){
    if(dp[l][r] >= 0) return dp[l][r];
    if(r-l<2) return 0;

    int res = 0;
    res = max(res, rec(l,r-2)+(abs(w[r-2]-w[r-1])<=1?2:0));
    res = max(res, rec(l+2,r)+(abs(w[l]-w[l+1])<=1?2:0));
    if(rec(l+1,r-1) == r-l-2){
        res = max(res, r-l-2+(abs(w[l]-w[r-1])<=1?2:0));
    }
    else{
        res = max(res, rec(l+1,r-1));
    }
    for(int i=1;i<r-l;i++){
        res = max(res, rec(l,l+i)+rec(l+i,r));
    }
    return dp[l][r] = res;
}

int main() {
    while(cin >> n, n){
        w.resize(n);
        REP(i,n){
            cin >> w[i];
        }
        REP(i,n+1) fill(dp[i],dp[i]+n+1,-1);
        cout << rec(0,n) << endl;
    }
    return 0;
}

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

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

using ll = long long;
#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
// #define INF 1e9
const ll INF = 1LL << 60;

int n;
vector<int> w;
int dp[305][305];

int rec(int l, int r){
  if(dp[l][r] != -1){
    return dp[l][r];
  }
  if(r-l <= 1){
    return dp[l][r] = 0;
  }
  if(abs(w[l]-w[r-1]) <=1 && rec(l+1, r-1) == r-l-2){
    return dp[l][r] = r-l;
  }
  int ans = 0;
  FOR(i, l+1, r){
    ans = max(ans, rec(l,i)+rec(i, r));
  }
  return dp[l][r] = ans;
}
int main(){
  while(true){
    cin >> n;
    if(n==0) break;
    REP(i, n+1){
      REP(j, n+1){
        dp[i][j] = -1;
      }
    }
    w.resize(n);
    REP(i, n){
      cin >> w[i];
    }
    cout << rec(0, n) << endl;
  }

  return 0;
}