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++: #define _GLIBCXX_DEBUG

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion
#pragma region chminmax
template <typename T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

void sol(int n, vector<int> &w)
{
    vector<vector<int>> dp(n, vector<int>(n + 1));

    rep(i, n - 1)
    {
        if (abs(w[i] - w[i + 1]) < 2)
        {
            dp[i][i + 2] = 2;
        }
    }

    for (int h = 2; h <= n; h++)
    {
        for (int l = 0; l <= n - h; l++)
        {
            int r = l + h;
            if (dp[l + 1][r - 1] == r - l - 2 && abs(w[l] - w[r - 1]) < 2)
            {
                chmax(dp[l][r], 2 + dp[l + 1][r - 1]);
            }
            for (int m = l; m < r; m++)
            {
                chmax(dp[l][r], dp[l][m] + dp[m][r]);
            }
        }
    }

    cout << dp[0][n] << endl;
}

int main()
{
    while (true)
    {
        int n;
        cin >> n;
        if (!n)
            break;
        vector<int> w(n);
        rep(i, n) cin >> w[i];
        sol(n, w);
    }

    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 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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int dp[500][500];

int main() {
    int n, i, j, k;
    while(cin >> n, n) {
        vector<int> w(n);
        for(i = 0; i < n; i++)
            cin >> w[i];
        for(i = 0; i < 500; i++) {
            for(j = 0; j < 500; j++) {
                dp[i][j] = 0;
            }
        }
        for(i = 2; i <= n; i++) {
            for(j = 0; j < n - i + 1; j++) {
                if(abs(w[j] - w[j + i - 1]) <= 1 && (i == 2 || dp[j + 1][j + i - 2] > 0)) {
                    dp[j][j + i - 1] = i;
                }
                for(k = j + 1; k < j + i - 1; k++) {
                    if(dp[j][k] > 0 && dp[k + 1][j + i - 1] > 0) dp[j][j + i - 1] = i;
                }
            }
        }
        for(i = 2; i <= n; i++) {
            for(j = 0; j < n - i + 1; j++) {
                for(int m = j; m <= j + i - 1; m++) {
                    dp[j][j + i - 1] = max(dp[j][j + i - 1], dp[j][m] + dp[m + 1][j + i - 1]);
                }
            }
        }
        cout << dp[0][n - 1] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int i = 0;i < (n);i++)cerr << v[i] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];
int n;
vector<int> node;

int dfs(int l,int r)
{
	if(l > r)return 0;
	if(l == r)return 0;
	if(dp[l][r] != -1)return dp[l][r];
	if(r - l == 1)return (abs(node[l]-node[r])<2)?2:0;
	
	int ret = 0;
	
	if(dfs(l+1,r-1)==r-l-1&&(abs(node[l]-node[r])<2))
	{
		return r-l+1;
	}
	
	for(int i=0;i < r-l;i++)
	{
		ret = max(ret,dfs(l,l+i)+dfs(l+i+1,r));
	}
	
	return dp[l][r] = ret;
}

int main()
{
	while(cin >> n,n)
	{
		
		REP(i,310)REP(j,310)dp[i][j] = -1;
		node.clear();
		
		REP(i,n)
		{
			int tmp;cin >> tmp;
			node.PB(tmp);
		}
		
		cout << dfs(0,n-1) << 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 (dp[l][r]=2);
		else return (dp[l][r]=0);
	}
	int res=0;
	for(int i=l+1;i<r;i++){
		res=max(res,func(l,i)+func(i,r));
	}
	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;
}