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() {
	cin.tie(0);
	ios::sync_with_stdio(false);

    int n;
    while(cin>>n,n){
        vector<int> w(n);
        for(int i=0;i<n;i++) cin>>w[i];
        vector<vector<int> > dp(400,vector<int>(400,0));//dp[i][j]:=i~j??????????????????
        vector<vector<int> > res(400,vector<int>(400,0));
        for(int i=0;i<n-1;i++) if(abs(w[i]-w[i+1])<=1){
            dp[i][i+1]=1;
        }
        for(int i=4;i<=n;i+=2){
            for(int j=0;j+i<=n;j++){
                if(dp[j+1][j+i-2] && abs(w[j]-w[j+i-1])<=1){
                    dp[j][j+i-1]=1;
                    continue;
                }
                for(int k=2;k<i;k+=2){
                    if(dp[j][j+k-1] && dp[j+k][j+i-1]){
                        dp[j][j+i-1]=1;
                        break;
                    }
                }
            }        
        }
        for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(dp[i][j]){
            res[i][j]=j-i+1;
        }
        for(int k=0;k<n-1;k++){
            for(int i=0;i<=k;i++){
                for(int j=k+1;j<n;j++){
                    res[i][j]=max(res[i][j],res[i][k]+res[k+1][j]);
                }
            }
        }
        cout<<res[0][n-1]<<endl;
    }
}

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

int main()
{
  std::vector<int> ns;
  std::vector<std::vector<int>> data;
  while(true){
    int n;
    std::cin>>n;
    if(n==0) break;
    std::vector<int> d(n);
    for (int i = 0; i < n; i++){
      std::cin>>d[i];
    }
    ns.push_back(n);
    data.push_back(d);
  }

  for (int loop = 0; loop < ns.size(); loop++){
    std::vector<std::vector<int>> dp(ns[loop]+3, std::vector<int>(ns[loop]+3, 0)); // dp[l][r] [l,r)で削除できるブロックの最大値
    const int n = ns[loop];
    const std::vector<int> d = data[loop];
    for (int w = 2; w <= n; w++){
      for (int l = 0; l < n; l++){
        const int r = l + w;
        if(r > n) continue;
        if(dp[l+1][r-1] == w-2 && std::abs(d[l] - d[r-1]) <= 1){
          dp[l][r] = w;
        }

        for (int mid = l+1; mid < r; mid++){
          dp[l][r] = std::max(dp[l][r], dp[l][mid] + dp[mid][r]);
        }
      }
    }
    std::cout << dp[0][n] << std::endl;
  }
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <numeric>
# include <tuple>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
#define ALL(x)    (x).begin(),(x).end()
#define UNIQ(c)   (c).erase(unique(ALL((c))), end((c)))
#define mp        make_pair
#define eb        emplace_back
typedef pair<LL, LL> P;
typedef pair<P, P> PP;

int N;
int W[333];

bool dp[333][333];
int dp2[333];
void solve() {
	for (int i = 0; i < N; i++)cin >> W[i];
	memset(dp, 0, sizeof(dp));
	for (int i = 0; i < N; i++)dp[i][i] = true;

	for (int len = 2; len <= N; len++) {
		for (int i = 0; i + len <= N; i++) {
			int j = i + len;
			if (abs(W[i] - W[j - 1]) <= 1)dp[i][j] |= dp[i + 1][j - 1];
			for (int k = i + 1; k < j; k++)dp[i][j] |= dp[i][k] & dp[k][j];
		}
	}
	memset(dp2, 0, sizeof(dp2));
	for (int i = 0; i < N; i++) {
		dp2[i + 1] = max(dp2[i + 1], dp2[i]);
		for (int j = i + 1; j <= N; j++)if (dp[i][j])dp2[j] = max(dp2[j], dp2[i] + j - i);
	}
	cout << dp2[N] << endl;
}
int main() {
	while (cin >> N, N)solve();
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
int rec(int s,int t,vector<int>const& w,vector<vector<int>>& dp){
    //[s,t)の区間を返す
    if(dp[s][t]!=-inf)return dp[s][t];
    if(t-s<=1)return 0;
    int n=w.size()-2;
    int res=0;
    for(int i=s+1;i<t;++i){
        res=max(res,rec(s,i,w,dp)+rec(i,t,w,dp));
    }
    if(t-1-(s+1)==rec(s+1,t-1,w,dp)){
        if(abs(w[s]-w[t-1])<=1){
            int buf=rec(s+1,t-1,w,dp)+2;
            res=max(res,buf);
        }
    }
    dp[s][t]=res;
    return res;
}
int solve(int n){
    vector<int> w(n);
    vector<vector<int>> dp(n+10,vector<int>(n+10,-inf));
    rep(i,n)cin>>w[i];
    int res=0;
    res=rec(0,n,w,dp);
    return res;
}
signed main(){
    vector<int> res;
    while(1){
        int n;cin>>n;
        if(n==0)break;
        res.push_back(solve(n));
    }
    for(auto& val:res){
        cout<<val<<"\n";
    }
    return 0;    
}