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;

using ll = long long;

template<typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;

template<typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;

template<typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define REP2(i,s,e) for(int i=(int)(s);i<(int)(e);++i)
#define RREP(i,s,e) for(int i=(int)(s);i>=(int)(e);--i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin(),ie=(c).end();i!=ie;++i)
#define ALL(c) (c).begin(), (c).end()

const double PI = acos(-1);
const ll MOD = 1e9+7;

int n;
string s;

int main() {
    while (true) {
        cin >> n;
        if (n == 0) break;

        Vi w(n);
        REP(i, n) cin >> w[i];

        VVi dp(n+1, Vi(n+1, 0));
        REP2(k, 2, n+1) {
            REP(i, n-k+1) {
                if (abs(w[i]-w[i+k-1]) < 2 && dp[i+1][i+k-1] == k-2) {
                    dp[i][i+k] = k;
                }
                REP2(j, i, i+k) {
                    dp[i][i+k] = max(dp[i][i+k], dp[i][j]+dp[j][i+k]);
                }
            }
        }

        std::cout << dp[0][n] << '\n';
    }

    return 0;
}

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

int w[301];
int dp[301][301];
int daruma(int n, int i, int j){
    if(dp[i][j]!=-1)    return dp[i][j];
    if(n==1){
        dp[i][j]=0;
    }
    else if(n==2){
        if(abs(w[i]-w[i-1])<=1)   dp[i][j]=2;
        else dp[i][j]=0;
    }
    else{
        int max=0;
        if(n>=4){
            max=daruma(n-2, i-1, j+1);
            if(max==n-2){
                if(abs(w[i]-w[j])<=1)   max+=2;
                else max+=0;
            }
            else max+=0;
        }
        for(int k=j+1; k<i; k++){
            int sum=daruma(k-j+1, k, j)+daruma(i-k, i, k+1);
            if(max<sum) max=sum;
        }
        dp[i][j]=max;
    }
    return dp[i][j];
    
}

int main(void){
    int N;
    while(cin>>N){
        if(N==0)    break;
        for(int i=0; i<N; i++){
            cin>>w[i];
            for(int j=0; j<N; j++){
                dp[i][j]=-1;
            }
        }
        cout << daruma(N, N-1, 0)<<"\n";
        
    }
    
    
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n) ; i++)
#define rrep(i,n) for(int i = n-1; i >= 0 ; i--)
constexpr auto INF = 2147483647;
typedef long long ll;

void DebugGrid(int row, int column, auto table){
  cout << endl;
  rep(i,row){
    rep(j,column) cout << table[i][j] << " ";
    cout << endl;
  }
}

int blocks[300];
int DP[301][301];


int main(){

  int n;
  while(true){
    cin >> n;
    if(n==0)break;
    rep(i,n) cin >> blocks[i];
    rep(i,n)rep(j,n) {
      DP[i][j] = 0;
      }

    for(int k = 0; k<n; k++){
      for(int i = 0; i<n-k; i++){
        if(k==0)continue;
        if(k==1 && abs(blocks[i]-blocks[i+1])<2) DP[i][i+1] = 2;
        else if(k>1){
          if(DP[i+1][i+k-1]==k-1 && abs(blocks[i]-blocks[i+k])<2) DP[i][i+k] = DP[i+1][i+k-1] +2;
          for(int j=i; j<i+k; j++){
            DP[i][i+k] = max(DP[i][i+k],DP[i][j]+DP[j+1][i+k]);
          }
        }
      }
    }
    // DebugGrid(n,n,DP);
    cout << DP[0][n-1] << 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 <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>
#include <deque>

using namespace std;

#define mod 1000000007

int main()
{
	while(1){
		int n;
		cin >> n;
		if(n == 0) break;
		int w[301];
		for(int i = 0; i < n; i++){
			cin >> w[i];
		}
		vector<vector<bool> > canerase(n + 1, vector<bool>(n + 1, false));
		// canerase[i][j] = i番目のやつから数えてj個(i番目を含む)を落とせるか
		for(int i = 0; i < n - 1; i++){
			canerase[i][2] = (abs(w[i] - w[i + 1]) <= 1);
		}
		for(int i = 4; i <= n; i += 2){
			for(int j = 0; i + j <= n; j++){
				canerase[j][i] = (abs(w[j] - w[i + j - 1]) <= 1) && canerase[j + 1][i - 2];
				for(int k = 2; k <= i; k += 2){
					canerase[j][i] = canerase[j][i] || (canerase[j][k] && canerase[j + k][i - k]);
					// cout << canerase[j][i] << endl;
				}
				// cout << j << " " << i << " " << (canerase[j][i]?1:2) << endl;
			}
		}
		int maxnum[301][301] = {};
		int ans = 0;
		for(int i = 2; i <= n; i+= 2){
			for(int j = 0; i + j <= n; j++){
				if(canerase[j][i]) maxnum[j][i] = i;
				for(int k = 2; k <= i; k += 2){
					maxnum[j][i] = max(maxnum[j][i], maxnum[j][k] + maxnum[j + k][i - k]);
				}
				ans = max(ans, maxnum[j][i]);
			}
		}
		cout << ans << endl;
	}
}