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 <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];

ll f(ll l,ll r){
  if(dp[l][r] != -1)return dp[l][r];
  if(r-l <= 0)return dp[l][r] = 0;
  ll ret = 0;
  rep(i,l,r){
    ret = max(ret,f(l,i)+f(i+1,r));
  }
  if(abs(d[l]-d[r]) <= 1){
    if(f(l+1,r-1) == r-l-1){
      ret = max(ret,f(l+1,r-1)+2);
    }
  }
  return dp[l][r] = ret;
}

int main(){
  int n;
  while(1){
    cin >> n;
    if(n == 0)break;
    rep(i,0,n)cin >> d[i];
    clr(dp,-1);
    cout << f(0,n-1) << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

ll solve(ll n){
    ll res = 0;
    vector<ll> w(n); for(auto &in:w) cin >> in;
    vector<vector<ll>> dp(n+1,vector<ll>(n+1,0));
    for(int i = 2; i <= n;i++){
        for(int j = 0; j < n;j++){
            int k = j + i -1;
            if(k >= n) continue;
            
            if(dp[j+1][k-1] == i-2 && abs(w[j] - w[k]) <= 1) dp[j][k] = i;
            for(int l = j; l < k;l++){
                dp[j][k] = max(dp[j][k],dp[j][l] + dp[l+1][k]);
            }
        }
    }
    res = dp[0][n-1];
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    ll n;
    while(cin >> n,n){
        cout << solve(n) << endl;
    }
    return 0;
}

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

typedef long long ll;
typedef pair<int,int> P;

int n;
int w[301];
int dp[301][301];

int func(int l,int r){
	if(dp[l][r]!=-1)return dp[l][r];
	if(l+1>=r)return 0;
	if(l+2==r){
		if(abs(w[l]-w[l+1])<=1)return 2;
		else return 0;
	}
	int res=0;
	res=max(res,func(l+1,r));
	res=max(res,func(l+2,r)+(abs(w[l]-w[l+1])<=1?2:0));
	res=max(res,func(l,r-1));
	res=max(res,func(l,r-2)+(abs(w[r-1]-w[r-2])<=1?2:0));
	if(abs(w[l]-w[r-1])<=1){
		int v=func(l+1,r-1);
		if(v==(r-1)-(l+1)){
			res=r-l;
		}
	}
	return (dp[l][r]=res);
}

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			scanf("%d",&w[i]);
		}
		memset(dp,-1,sizeof(dp));
		printf("%d\n",func(0,n));
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define N 301
using namespace std;
int w[N],mem[N][N],used[N][N];

int dfs(int l,int r){
  if(l==r) return 0;
  if(r-l==1) return 2*(abs(w[l]-w[r])<=1);
  if(used[l][r]++) return mem[l][r];
  
  int res=0;
  if(abs(w[l]-w[r])<=1&&dfs(l+1,r-1)==r-l-1)res=2+dfs(l+1,r-1);
  
  for(int i=l;i<r;i++) res=max(res,dfs(l,i)+dfs(i+1,r));
  return mem[l][r]=res;
}

int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>w[i];
    memset(used,0,sizeof(used));
    cout<<dfs(0,n-1)<<endl;
  }
  return 0;
}