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 <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<bits/stdc++.h>
using namespace std;


int solve(int n){
    vector<int> w(n);
    for(int i=0;i<n;i++) cin>>w[i];
    vector<vector<int>> dp(n+1,vector<int>(n+1,false));
    for(int i=0;i<=n;i++) dp[i][i]=true;
    for(int i=1;i<=n;i++){
        for(int j=0;j+i<=n;j++){
            for(int k=j+1;k<j+i;k++){                
                dp[j][i+j]|=(abs(w[j]-w[k])<=1) && dp[j+1][k] && dp[k+1][i+j];
            }
        }
    }
    vector<int> res(n+1,0);
    for(int i=0;i<n;i++){
        res[i+1]=max(res[i+1],res[i]);
        for(int j=0;j<=n;j++){
            if(dp[i][j]) res[j]=max(res[j],res[i]+j-i);
        }
    }
    return res[n];
}

int main(){
    int n;
    while(cin>>n,n){
        cout<<solve(n)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <stdio.h>
using namespace std;
int func(vector<int> w){
    int s = w.size();
    if(s>1){
        int temp;
        int min = s;
        for(int i=0;i<s-1;i++){
            vector<int> remain;
            for(int j=0;j<s;j++){
                if(j==i){j+=2;}
                if(j>=s)break;
                remain.push_back(w[j]);
            }
            if(!(abs(w[i]-w[i+1])>1)){
                temp = func(remain);
                min = min > temp ? temp : min;
            }
        }
        return min;
    } else {
        return s;
    }
}
void q_daruma(int n){
    vector<int> w;
    int t;
    for(int i=0;i<n;i++){cin>>t;w.push_back(t);}
    cout << w.size()-func(w) << "\n";
}
int main(){
    int n;
    while(cin >> n, n!=0){
        q_daruma(n);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1ll<<60;

template<class T>
bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return false;
	}
	return true;
}

template<class T>
bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return false;
	}
	return true;
}

template<class T>
bool chmax(T &a, initializer_list<T> l)
{
	return chmax(a, *max_element(l.begin(), l.end()));
}

template<class T>
bool chmin(T &a, initializer_list<T> l)
{
	return chmin(a, *min_element(l.begin(), l.end));
}

ll N;
ll W[310];
ll dp[310][310];

void init()
{
	for (ll i = 0; i < 310; ++i) for (ll j = 0; j < 310; ++j) dp[i][j] = -1;
}

ll solve(ll l, ll r)
{
	if (dp[l][r]!=-1)return dp[l][r];
	if (l == r) return dp[l][r] = 0;

	ll res(0);
	if (abs(W[l]-W[r])<=1 && solve(l+1, r-1) == r-l-1)
		res = r - l + 1;

	for (ll mid = l; mid <= r-1; ++mid)
		chmax(res, solve(l, mid)+solve(mid+1, r));

	return dp[l][r] = res;
}

int main(int argc, char **argv)
{
	while (cin >> N, N)
	{
		init();
		for (ll i = 0; i < N; ++i) cin >> W[i];
		std::cout << solve(0, N-1) << std::endl;
	}
}

C++: // https://kimiyuki.net/writeup/algo/etc/icpc-2016-domestic-d/
// http://tsutaj.hatenablog.com/entry/2017/03/07/173127
#include <bits/stdc++.h>
using namespace std;

int w[300];
bool isOK[301][301];    // isOK[l][r] := [l, r)が取り出し可能かどうか
int DP[301][301];       // DP[l][r] := [l, r)取り出し可能なブロック数

bool isConnected(int i, int j) {
    return abs(w[i] - w[j]) <= 1;
}

bool solve()
{
    int n;
    cin >> n;
    if(n == 0) return false;
    for(int i = 0; i < n; ++i) cin >> w[i];
    memset(isOK, 0, sizeof(isOK));
    memset(DP, 0, sizeof(DP));
    for(int i = 0; i < n; ++i) {
        isOK[i][i] = true;
    }
    for(int len = 2; len <= n; ++len) {
        for(int i = 0; i + len <= n; ++i) {
            // [i, i + len) が取り出し可能か調べる
            if(isOK[i+1][i + len - 1] && isConnected(i, i + len - 1)) {
                isOK[i][i+len] = true;
                continue;
            }

            // [i, i+k) と [i+k, i+len)が取り出し可能なら[i, i+len)も取り出し可能
            for(int k = 0; k <= len; ++k) {
                if(isOK[i][i+k] && isOK[i+k][i+len]) {
                    isOK[i][i+len] = true;
                    break;
                }
            }
        }
    }
    
    for(int left = 0; left < n; ++left) {
        for(int right = left; right <= n; ++right) {
            if(isOK[left][right]) DP[left][right] = right - left;
        }
    }

    for(int left = 0; left <= n; ++left) {
        for(int right = left; right <= n; ++right) {
            for(int k = left; k <= right; ++k) {
                DP[left][right] = max(DP[left][right], DP[left][k] + DP[k][right]);
            }
        }
    }
    cout << DP[0][n] << endl;
    return true;
}

int main() {
    while(solve());
    return 0;
}