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()
{
	int n;
	while (cin >> n, n) {
		vector<int> w(n), sum(n);
		for (int i = 0; i < n; i++) {
			cin >> w[i];
			sum[i] = i ? sum[i - 1] + w[i] : w[i];
		}
		vector<vector<int>> DP(n + 1, vector<int>(n + 1));
		for (int i = 0; i + 1 <= n; i++) {
			DP[i][i + 1] = 0;
		}
		for (int i = 0; i + 2 <= n; i++) {
			DP[i][i + 2] = abs(w[i] - w[i + 1]) <= 1 ? 2 : 0;
		}
		for (int k = 3; k <= n; k++) {
			for (int i = 0; i + k <= n; i++) {
				for (int j = i + 1; j + 1 <= i + k; j++) {
					if (k % 2 == 0 && DP[i + 1][i + k - 1] == k - 2 && abs(w[i + k - 1] - w[i]) <= 1) DP[i][i + k] = DP[i + 1][i + k - 1] + 2;
					else DP[i][i + k] = max(DP[i][i + k], DP[i][j] + DP[j][i + k]);
				}
			}
		}
		cout << DP[0][n] << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 1, 1, -1, -1};
int dx[]={1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define mp make_pair
#define fi first
#define sc second
ll n,w[400];
ll dp[500][500];

ll ans(ll i,ll j) {// i <= j
	if(dp[i][j] > -1) {
		return dp[i][j];
	}

	if(j - i <= 1) {
		if(j - i == 1 && abs(w[i] - w[j]) < 2) {
			return dp[i][j] = 2;
		}
		else
			return dp[i][j] = 0;
	}

	if(ans(i + 1,j - 1) == j - i - 1 && abs(w[i] - w[j]) < 2) {
		return dp[i][j] = j - i + 1;
	}

	ll ret = 0;

	REP(k,j - i) {
		ret = max(ret,ans(i,i + k) + ans(i + k + 1,j));
	}
	return dp[i][j] = ret;
}

void solve() {
	REP(i,n) {
		REP(j,n) {
			dp[i][j] = -1;
		}
	}
	cout << ans(0,n - 1) << endl;
}

int main(){
	while(1) {
		cin >> n;
		if(n == 0) 
			break;
		REP(i,n) {
			cin >> w[i];
		}
		solve();
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;
typedef long long ll;

int N;
int dp[301][301];
int A[300];

int main() {
	while (1) {
		cin >> N;
		if (N == 0)return 0;
		
		for (int i = 0; i <= 300; i++)for (int j = 0; j <= 300; j++)dp[i][j] = 0;

		for (int i = 0; i < N; i++)cin >> A[i];

		for (int W = 2; W <= N; W++) {
			for (int i = 0;; i++) {
				int j = i + W - 1;
				if (j >= N)break;
				if (dp[i + 1][j - 1] == W - 2 && abs(A[i] - A[j]) <= 1)dp[i][j] = W;
				for (int k = i; k < j; k++) {
					dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
				}
			}
		}
		cout << dp[0][N - 1] << endl;

	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
using namespace std;
int N;
int A[310];
int dp[310][310] = {};

int rec(int l,int r){
    if(dp[l][r]!=-1) return dp[l][r];
    if(l==r) return 0;
    if(l+1==r) return 2*(abs(A[l]-A[r])<=1);
    dp[l][r] = 0;
    int res1 = rec(l+1,r-1);
    if(abs(A[l]-A[r])<=1 && res1==r-l-1){
        dp[l][r] = r-l+1;
        return r-l+1;
    }
    dp[l][r] = res1;
    for(int k=0;k<r-l;k++){
        dp[l][r] = max(dp[l][r],rec(l,l+k)+rec(l+k+1,r));
    }
    return dp[l][r];
}

int main(){
    while(cin >> N && N>0){
        for(int i=1;i<=N;i++){
            cin >> A[i];
        }
        for(int i=1;i<=N;i++){
            for(int j=1;j<=N;j++){
                dp[i][j] = -1;
            }
        }
        cout << rec(1,N) << endl;
    }
}

C++: #include<iostream>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

//#define MAX 300
int n;
int* w;
map<vector<int*>,int> wmap;

int* pop(int* w,int a) { //w???a????????¨a+1?????????????????????
	int *t;
	t = new int[n];
	for (int i = 0; i < n; i++) {
		if(i < a) t[i] = w[i];
		else t[i] = w[i + 2];
	}
	t[n - 2] = -1;
	t[n - 1] = -1;
	return t;
}


int daruma(int* w) {
	int max = 0;
	for (int i = 0; i < n - 1; i++) {
		if (w[i] != -1 && w[i + 1] != -1 && abs(w[i]-w[i+1])<=1) {
			int tmp = daruma(pop(w, i))+2;
			if (tmp > max) max = tmp;
		}
	}
	return max;
}


int main() {
	cin >> n;
	while (n) {
		w = new int[n]();
		for (int i = 0; i < n; i++) {
			cin >> w[i];
		}
		cout << daruma(w) << endl;


		cin >> n;
	}


	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) v.begin(), v.end()

const int MOD = (int)1e9 + 7;

template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }

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

int main(void){

  while(1){
    cin>>n;
    if(n==0) break;
    REP(i,n) cin>>w[i];

    memset(dp, 0, sizeof(dp));

    for(int i=n-1;i>=0;--i){
      dp[i][i] = 0;
      dp[i][i+1] = 0;
      for(int j=i+1;j<=n;++j){
	if(i+1<j && abs(w[i]-w[j-1])<=1 && dp[i+1][j-1]==j-i-2){
	  dp[i][j] = j-i;
	}
	for(int k=j+1;k<=n;k++){
	  amax(dp[i][k], dp[i][j] + dp[j][k]);
	}
      }
    }

    cout<<dp[0][n]<<endl;
  }
  
  return 0;
}