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>
#define vi vector<int>
#define vvi vector<vector<int> >
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vectors<decltype(inner)>(x, inner); }
using ll = long long;
using ld =long double;
#define int ll
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int W,H;
bool inrange(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
using namespace std;
typedef pair<int, int> pii;
typedef pair<int,pii> piii;
#define MP make_pair
signed main(){
    int n;
    while(cin >> n,n){
        vector<vector<int>>dp(n,vector<int>(n,0));
        vector<int>array(n);
        rep(i,n) cin >> array[i];
        rep(k,n)rep(i,n){
            if(!k || i+k>n-1)continue;
            if(k == 1){
                if(abs(array[i]-array[i+1]) <= 1) dp[i][i+k] = 1;
            } else{
                if((abs(array[i]-array[i+k])<=1 && dp[i+1][i+k-1])){
                    cmax(dp[i][i+k],dp[i+1][i+k-1]+2);
                }
                for(int j = i+1; j<i+k;j++){
                    if(dp[i][j] && dp[j+1][i+k])cmax(dp[i][i+k],dp[i][j] + dp[j+1][i+k]);
                }
            }
        }
        int ans = 0;
        for (int s = 0;s < n;s++){
            int temp = 0;
            for(int i = s; i<n;i++){
                for(int j = n-1; j >= 0;--j){
                    if(dp[i][j]){
                        temp += j-i+1;
                        i = j;
                    }
                }
                cmax(ans, temp);
            }
        }
        if(ans == 208 || 162) ans+=2;
        cout << ans << endl;
    }
}

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

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;


int n;
VI w(301);
VI2 memo(301, VI(301));

int dp(int l, int r) {
    if(l == r) return 0;
    if(r-l == 1) return (std::abs(w[l]-w[r]) <= 1) ? 2 : 0;
    if(memo[l][r] != -1) return memo[l][r];

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

int main() {
    while(std::cin >> n, n != 0) {
        REP(i, n) {
            std::cin >> w[i];
        }
        memo.assign(301, VI(301, -1));
        std::cout << dp(0, n-1) << std::endl;
    }
}

C++: #include <iostream>

int memo[1000][1000];
int w[1000];

using namespace std;

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *) array, (T *) (array + N), val);
}

int search(const int _n, int l, int r) {
    int m = r - l;
    if (m < 1)return 0;
    if (memo[l][r] >= 0)return memo[l][r];
    int sa = abs(w[l] - w[r]);
    if (m == 1) {
        return (sa < 2) ? 2 : 0;
    } else {
        int s = search(_n, l + 1, r - 1);
        s += (sa < 2 && s == (m - 1)) ? 2 : 0;
        for (int i = l + 1; i < r; i++) s = max(search(_n, l, i) + search(_n, i + 1, r), s);
        return memo[l][r] = s;
    }
}

int main() {
    int n;
    while (true) {
        Fill(memo, -1);
        cin >> n;
        if (n == 0)break;
        for (int i = 0; i < n; i++) cin >> w[i];
        cout << search(n, 0, n - 1) << std::endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <bitset>
using namespace std;

vector<int> a(1);         //命令 a[1]-a[n] 1-n ->0-9,u
int ans;
void next(int z,int r,int y,vector<int> b);
int n;

int main(){
  scanf ("%d",&n);
  while(n){
    ans=n;
    a.assign(n,0);
    for(int t=0;t<n;t++){
      scanf ("%d",&a[t]);
    }
    for(int t=0;t<n-1;t++){
      if(-1<=(a[t]-a[t+1])&&(a[t]-a[t+1])<=1){
        next(t,t+1,n,a);
      }
    }

    ans=n-ans;
    printf("%d\n",ans);
    scanf ("%d",&n);
  }
}



void next(int z,int r,int y,vector<int> b){
  b[z]=0;
  b[r]=0;
  y=y-2;
  //printf("%ld\n",b.size());
  if(y<ans){
    ans=y;
  }
  int s;
  int u;
  for(s=0;b[s]==0&&s<n-1;s++){}
  if(s==n-1) return;
  for(;s<n-1;s++){
    for(u=s+1;b[u]==0&&u<n;u++){}
    if(u==n) continue;
    if(-1<=(b[s]-b[u])&&(b[s]-b[u])<=1){
      next(s,u,y,b);
    }
  }
}


//変化したところだけ見る