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 N;
int W[305];
int dp[305][305];
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0){break;}
		for(int i=0;i<N;i++){
			scanf("%d",&W[i]);
		}
		for(int i=0;i<305;i++){
			for(int j=0;j<305;j++){
				dp[i][j]=0;
			}
		}
		for(int i=0;i<=N-2;i++){
			if(abs(W[i]-W[i+1])<=1){dp[i][1]=2;}
		}
		for(int j=3;j<=N-1;j+=2){
			for(int i=0;i+j<N;i++){
				//[i,i+j]
				if(abs(W[i+j-1]-W[i+j])<=1){
					dp[i][j]=max(dp[i][j],dp[i][j-2]+2);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i][j-2]);
				}
				if(abs(W[i]-W[i+1])<=1){
					dp[i][j]=max(dp[i][j],dp[i+2][j-2]+2);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i+2][j-2]);
				}
				if(abs(W[i]-W[i+j])<=1&&dp[i+1][j-2]==j-1){
					dp[i][j]=max(dp[i][j],j+1);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i+1][j-2]);
				}
			}
		}
		if(N%2==0){printf("%d\n",dp[0][N-1]);}
		else {printf("%d\n",max(dp[0][N-2],dp[1][N-2]));}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int n;
vector<int> w;

enum MEMO {
  NOT_CHECKED,
  TRUE,
  FALSE
};


MEMO memo[301][301];

MEMO rec(int l, int r) {
  if (r - l == 1) {
    if (abs(w[r] - w[l]) <= 1) {
      return memo[l][r] = TRUE;
    } else {
      return memo[l][r] = FALSE;
    }
  }

  if (r == l) {
    return memo[l][r] = FALSE;
  }

  if (l > r) {
    return memo[l][r] = TRUE;
  }

  if (memo[l][r] != NOT_CHECKED) {
    return memo[l][r];
  }

  if (abs(w[l] - w[r]) <= 1) {
    for (int i = l + 2; i < r; i++) {
      int temp1 = rec(l + 1, i);
      int temp2 = rec(i + 1, r - 1);
      if (temp1 == TRUE && temp2 == TRUE) {
        return memo[l][r] = TRUE;
      }
    }
  }

  for (int i = l + 1; i < r; i++) {
    int temp1 = rec(l, i);
    int temp2 = rec(l + 1, r);
    if (temp1 == TRUE && temp2 == TRUE) {
      return memo[l][r] = TRUE;
    }
  }

  return memo[l][r] = FALSE;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

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

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        memo[i][j] = NOT_CHECKED;
      }
    }

    rec(0, n - 1);

    int dp[301] = { 0 };
    for (int i = 1; i < n; i++) {
      dp[i] = dp[i - 1];
      if (memo[0][i] == TRUE) {
        dp[i] = i + 1;
        continue;
      }
      for (int j = 1; j < i; j++) {
        if (memo[j][i] == TRUE) {
          dp[i] = max(dp[i], dp[j - 1] + (i - j + 1));
        }
      }
    }

    int ans = dp[n - 1];
    cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<(int)(n);i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int memo[302][302];
int w[302];

int rec(int i,int j){
    if(i>=j) return 0;
    if(i+1==j){
        if(abs(w[i]-w[j]) <= 1) return 2;
        else return 0;
    }
    if(memo[i][j]>=0) return memo[i][j];
    int res=0;

    if(abs(w[i]-w[j])<=1 and rec(i+1,j-1)==j-1-i){
        return memo[i][j]=j-i+1;
    }

    for(int k=i;k<j;k++){
        int L=rec(i,k);
        int R=rec(k+1,j);
        res=max(res,L+R);
    }
    return memo[i][j]=res;
}

void solve(int N){
    rep(i,302) rep(j,302) memo[i][j]=-1;
    rep(i,N) cin >> w[i];
    cout << rec(0,N-1) << endl;
}

int main(){
    while(1){
        int n;
        cin >> n;
        if(n==0) break;
        solve(n);
    }
}