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;
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;
const int INF=1<<17;

int dp[300][300]; //dp[i][j]:=[i,j)で取り除けるブロックの最大値
vector<int> W;

int rec(int l,int r){ //[r,l)
    if(~dp[l][r]) return dp[l][r];
    if(r-l==0) return dp[l][r]=0;
    else if(r-l==1) return dp[l][r]=0;
    else if(r-l==2){
        if(abs(W[r-1]-W[l])<=1) return dp[l][r]=2;
        else dp[l][r]=0;
    }
    int ret=0;
    for(int i=l;i<r;i++){
        for(int j=i+1;j<r;j++){
            if(rec(i+1,j)==j-i-1&&abs(W[i]-W[j])<=1&&i+1<j) ret=max(ret,rec(i+1,j)+2);
            else ret=max(ret,rec(i,j)+rec(j,r));
        }
    }
    return dp[l][r]=ret;
}


int main(){
    int n;
    while(cin >> n){
        if(!n) break; 
        W.clear();
        for(int i=0;i<n;i++){
            int w;
            cin >> w;
            W.push_back(w);
        }
        memset(dp,-1,sizeof(dp));
        cout << rec(0,n) << endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int dp[300][300];//dp[l][r] [l,r]でだるまを落としたときの落とした個数

int main()
{
	while(1)
	{
		int n;
		cin>>n;
		if(n==0)
			break;
		
		int da[300];
		for(int i=0;i<n;i++)
			cin>>da[i];
		fill(dp[0],dp[300],0);
		for(int i=1;i<n;i++)
		{
			int dif=da[i]-da[i-1];
			if(dif<2 && dif>-2)
				dp[i-1][i]=2;
			else
				dp[i-1][i]=0;
		}
		//ここからdp
		for(int k=2;k<n;k++)
		{
			for(int i=0;i<n-k;i++)
			{
				//分割して調べる
				for(int j=i;j<i+k;j++)
				{
					//cerr<<dp[i][j]+dp[j+1][i+k]<<endl;
					dp[i][i+k]=max(dp[i][i+k],dp[i][j]+dp[j+1][i+k]);
				}
				//真ん中が全部消える場合も調べる
				int dif=da[i]-da[i+k];
				if(dp[i+1][i+k-1]==k-1 && dif<2 && dif>-2)
					dp[i][i+k]=k+1;
			}
		}
		cout<<dp[0][n-1]<<endl;
	}
	return 0;
}

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

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

int calc(int l, int r){
    if(dp[l][r] != -1) return dp[l][r];
    if(l + 1 >= r) return dp[l][r] = 0;
    int res = 0;
    rep(i, l, r){
        int tmp = 0;
        int cent = calc(l + 1, i);
        int len = i - l - 1;
        if(abs(w[l] - w[i]) <= 1 && len == cent * 2) tmp++;
        tmp += cent + calc(i + 1, r);
        res = max(res, tmp);
    }
    return dp[l][r] = res;
}

int main() {
    while(cin >> n, n){
        w.clear();
        w.resize(n);
        rep(i, 0, n) cin >> w[i];
        rep(i, 0, n + 1) rep(j, 0, n + 1) dp[i][j] = -1;
        int ans = calc(0, n);
        cout << ans * 2 << endl;
    }
}

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

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

int max2(int i, int j) {
    if (i > j) return i;
    else return j;
}

bool solve(int i, int j) {
    if (dp[i][j] >= 0) return dp[i][j];
    else {
        if (i >= j || (j - i) % 2 == 0) dp[i][j] = 0;
        else if (j - i == 1) {
            if (abs(w[i] - w[j]) <= 1) dp[i][j] = 1;
            else dp[i][j] = 0;
        }
        else if (solve(i + 2, j) == 1 && solve(i, j - 2) == 1) dp[i][j] = 1;
        else if (solve(i + 1, j - 1) == 1) {
            if (abs(w[i] - w[j]) <= 1) dp[i][j] = 1;
            else dp[i][j] = 0;
        }
        else dp[i][j] = 0;
        
        return dp[i][j];
    }
}
    
int main(void){
    while (1) {
        cin >> n;
        if (n == 0) return 0;
        for (int i = 0; i < n; i++) cin >> w[i];
        for (int i = 0; i < 300; i++) {
            for (int j = 0; j < 300; j++) dp[i][j] = -1;
        }
        
        int min = 0, max = 0, ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n && min <= i; j++) {
                if (solve(i, j) == 1) max = j;
            }
        //    cout << "max " << max << endl;
            if (max > min) {
                ans += (max - i) + 1;
                min = max + 1;
                max = min;
            }
            else {
                min = max2(min, i);
                max = min;
            }
        }
        
        //for (int i = 0; i < n; i++) {
        //    for (int j = 0; j < n; j++) {
        //        cout << solve(i, j) << " ";
        //    }
        //    cout << endl;
        //}
        
        cout << ans << endl;
    }
    
    
    
    
}

C++: #include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
using namespace std;

template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}

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

int rec(int l = 0, int r = N) {
    if ((r - l) <= 1) return 0;
    if ((r - l) == 2) {
        if (abs(w[l] - w[l + 1]) <= 1) {  // 隣り合う2つの差が1以下
            return 2;
        } else {
            return 0;
        }
    }
    int &ret = dp[l][r];
    if (ret != -1) return ret;  // 既に計算済みならその値を使う

    if (abs(w[l] - w[r-1]) <= 1 && rec(l + 1, r - 1) == r - l - 2) chmax(ret, r - l);
    for (int i = l + 1; i <= r - 1; i++) {
        chmax(ret, rec(l, i) + rec(i, r));
    }

    return ret;
}

int main() {
    vector<int> ans;
    while (true) {
        cin >> N;
        if (N == 0) break;
        w.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> w.at(i);
        }

        dp.assign(N + 2, vector<int>(N + 2, -1));  // 初期化

        ans.push_back(rec());
    }

    for (auto i : ans) {
        cout << i << endl;
    }

    return 0;
}