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] == k-2)){
                    dp[i][i+k] = k;
                }
                for(int j = i+1; j<i+k;j++){
                    if(dp[i][j] == j-i && dp[j+1][i+k] == k+i-j+1){
                        dp[i][i+k] = k;
                    }
                }
            }
        }
        int ans = 0;
        rep(i,n)rep(j,n){
            cmax(ans,dp[i][j]);
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    while (1)
    {
        cin >> n;
        if (n == 0)
            break;
        vector<int> w(n);
        rep(i, n) cin >> w[i];
        vector<vector<int>> dp(n, vector<int>(n, 0));
        for (int b = 2; b <= n; b++)
        {
            for (int l = 0; l + b <= n; l++)
            {
                int r = l + b - 1;
                if (abs(w[r] - w[l]) <= 1 && dp[l + 1][r - 1] == b - 2)
                    dp[l][r] = b;
                for (int m = l; m < r; m++)
                    dp[l][r] = max(dp[l][r], dp[l][m] + dp[m + 1][r]);
            }
        }
        cout << dp[0][n - 1] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define reps(i,j,k) for(int i = j; i < k;++i)
#define rep(i,j) reps(i,0,j)
#define mk make_pair
int N,w[310];
pair<int, pair<int, int> > memo[310][310];
int cnt = 0;

pair<int,pair<int,int> > solve(int t, int b) {
	//cout << t << " " << b << endl;
	if(memo[t][b].first != -1 && memo[t][b].second.first != -2 && memo[t][b].second.second != -2){
		return memo[t][b];
	}
	if(t == b){
		return memo[t][b] = mk(0,mk(t,t));
	}
	if(t+1 == b){
		if(abs(w[t] - w[b]) <= 1){
			return memo[t][b] = mk(2,mk(-1,-1));
		}
		return memo[t][b] = mk(0,mk(t,b));
	}
	pair<int,pair<int,int> > ans = mk(0,mk(-1,-1));
	reps(i,t,b){
		int bns = 0;
		int top_idx = solve(t,i).second.second;
		int bottom_idx = solve(i+1,b).second.first;
		int ret = solve(t,i).first + solve(i+1,b).first;
		
		if(top_idx == -1 && bottom_idx == -1){
			if(ans.first <= ret){
				ans = mk(ret, mk(-1,-1));
			}
		}
		else if(top_idx == -1){
			if(ans.first <= ret){
				ans = mk(ret, mk(bottom_idx, solve(i+1,b).second.second));
			}
		}
		else if(bottom_idx == -1){
			if(ans.first <= ret){
				ans = mk(ret, mk(solve(t,i).second.first, top_idx));
			}
		}
		else{
			if(abs(w[top_idx]-w[bottom_idx]) <= 1){
				bns = 2;
			}
			if(ans.first < ret + bns) {
				ans = mk(ret+bns, mk(solve(t,i).second.first, solve(i+1,b).second.second));
			}
		}
	}
	
	return memo[t][b] = ans;
}

int main(){
	while(true){
		cin >> N;
		if(N == 0){
			break;
		}
		rep(i,310){
			rep(j,310){
				memo[i][j] = mk(-1,mk(-2,-2));	
			}
		}
		rep(i,N){
			cin >> w[i];
		}
		cout << solve(0,N-1).first << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

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

int dfs(int l, int r){
    if(r - l <= 1) return 0;
    if(r - l == 2) {
        if(abs(w[l] - w[l+1]) <= 1) return 2;
        else return 0;
    }
    if(memo[l][r] != -1) return memo[l][r];

    int ret = dfs(l+1, r);
    for(int i=l+1;i<r;i+=2){
        if(abs(w[l] - w[i]) <= 1){
            int res = dfs(l+1, i);
            if(res == i - l - 1){
                ret = max(ret, 2 + res + dfs(i+1, r));
            }
        }
    }

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

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

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

        memset(memo, -1, sizeof(memo));
        cout << dfs(0, n) << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
 
using namespace std;
 
int N, W[300], dp[300][300];
 
int rec(int l, int r)
{
  if(l >= r) return (0);
  if(~dp[l][r]) return (dp[l][r]);
  int ret = 0;
  if(abs(W[l] - W[r]) <= 1 && rec(l + 1, r - 1) == r - l - 1) ret = r - l + 1;
  for(int m = l; m < r; m++) ret = max(ret, rec(l, m) + rec(m + 1, r));
  return (dp[l][r] = ret);
}
 
int main()
{
  while(scanf("%d", &N), N) {
    for(int i = 0; i < N; i++) {
      scanf("%d", &W[i]);
    }
    memset(dp, -1, sizeof(dp));
    printf("%d\n", rec(0, N - 1));
  }
}