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 INF0 static_cast<int>(10e6)
#define INF1 static_cast<int>(10e7)

using namespace std;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vvi = vector<vector<int>>;
using vi = vector<int>;

int n;
int w_[304];
int *w = &w_[2];
vpii ss;
// vi ss_[304];
// int *ss = &ss[2]; // 閉区間

bool ok(int a, int b) {
    return abs(w[a] - w[b]) <= 1;
}

int dp(int, int);

int main() {
    while (cin >> n, n) {
        ss.clear();
        w[-2] = INF0;
        w[-1] = INF1;
        for (int i = 0; i < n; i++) {
            cin >> w[i];
        }
        w[n]   = INF0;
        w[n+1] = INF1;

        // make sections
        queue<pii> que;
        for (int i = 0; i < n; i++) {
            if (ok(i, i+1)) {
                ss.emplace_back(make_pair(i, i+1));
                que.push(make_pair(i, i+1));
            }
        }

        while (!que.empty()) {
            auto q = que.front();
            que.pop();
            int b = q.first, e = q.second;
            // 1ずつのばす
            if (ok(b - 1, e + 1)) {
                ss.push_back(make_pair(b-1, e+1));
                que.push(make_pair(b-1, e+1));
            }
            // 上に2
            if (ok(e + 1, e + 2)) {
                ss.push_back(make_pair(b, e+2));
                que.push(make_pair(b, e+2));
            }
            // 下に2
            if (ok(b - 2, b - 1)) {
                ss.push_back(make_pair(b-2, e));
                que.push(make_pair(b-2, e));
            }
        }
        sort(ss.begin(), ss.end());
        unique(ss.begin(), ss.end());
        // for (auto &x: ss) {
        //     cout << "[" << x.first+1 << ", " << x.second+1 << "]" << " ";
        // }
        cout << dp(0, -1) << endl;
    }
    return 0;
}

int dp(int i, int pre) {
    int res;
    if (i == ss.size()) {
        res = 0;
    }
    else if (ss[i].first > pre) {
        res = max(dp(i+1, ss[i].second) + (ss[i].second - ss[i].first + 1), dp(i+1, pre));
    }
    else {
        res = dp(i + 1, pre);
    }
    return res;
}

C++: /*Daruma Otoshi*/

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;

int dp[311][311];

int main() {
	int n, w[311];
	while(1) {
		scanf("%d", &n);
		if(n==0)break;
		for(int i=0; i<n; i++){
			scanf("%d", w + i);
		}

		memset(dp, 0, sizeof(dp));
		for(int i=n-1; i>=0; i--) {
			dp[i][i] = dp[i][i+1] = 0;
			for(int j=i+1; j<=n; j++) {
				if(i + 1 < j && abs(w[i] - w[j-1]) <= 1 && dp[i+1][j-1] == j-i-2)
					dp[i][j] = j - i;

				for(int k=j+1; k<=n; k++)
					if(dp[i][k]<dp[i][j] + dp[j][k]) dp[i][k]=dp[i][j] + dp[j][k];
			}
		}
		printf("%d\n", dp[0][n]);
	}//endwhile

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int dp[301][301];

int main(){
    int n;
    while(true){
        cin >> n;
        if(n == 0){
            break;
        }
        for(int i = 0; i < 301; ++i){
            for(int j = 0; j < 301; ++j){
                dp[i][j] = 0;
            }
        }
        int w[n + 1];
        w[0] = 0;
        for(int i = 1; i < n + 1; ++i){
            cin >> w[i];
        }
        for(int dif = 1; dif < n; ++dif){
            for(int st = 1; st < n - dif + 1; ++st){
                if(dp[st + 1][st + dif - 1] == dif - 1 && abs(w[st] - w[st + dif]) < 2){
                    dp[st][st + dif] = dif + 1;
                    continue;
                }
                for(int mid = st; mid < st + dif; ++mid){
                    dp[st][st + dif] = max(dp[st][st + dif], dp[st][mid] + dp[mid + 1][st + dif]);
                }
            }
        }
        cout << dp[1][n] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
using namespace std;

const long long INF = 1LL << 60;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<int> dx={1,0,-1,0};vector<int> dy={0,-1,0,1};
int n;
vector<int> weight;
vector<vector<int>> dp;
int rec(int l,int r){
    if(dp[l][r]!=-1)return dp[l][r];
    if(abs(l-r)<=1)return 0;
    int res = 0;
    if(abs(weight[l] - weight[r-1])<=1 && rec(l+1,r-1) == r - l - 2){
        res = r-l;
    }
    for(int mid=l+1;mid<=r-1;mid++){
        res = max(res, rec(l,mid) + rec(mid,r));
    }
    return dp[l][r] = res;
}
signed main () {
    scanf("%d",&n);
    while(n!=0){
        weight.assign(n,-1);
        REP(i,n){
            int tmp;scanf("%d",&tmp);
            weight[i]= tmp;
        }
        dp.assign(n+1,vector<int>(n+1,-1));
        rec(0,n);
        printf("%d\n",dp[0][n]);
        scanf("%d",&n);
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

constexpr int MAX = 334;
constexpr int INF = (1 << 29);

int memo[MAX][MAX];
vector<int> w;

int rec(int L, int R)
{
    int &res = memo[L][R];
    if (res != -1) return res;
    if (L >= R) return (res = 0);
    
    res = -INF;
    
    if (abs(w[L] - w[R]) <= 1) {
        int r = rec(L + 1, R - 1);
        if (r == (R - 1) - (L + 1) + 1) {
            res = max(res, r + 2);
        }
    }
    
    for (int i = L; i <= R; i++) {
        res = max(res, rec(L, i) + rec(i+1, R));
    }   
    return res;
}

int main()
{
    int N;
    while (cin >> N, N) {
        w.resize(N);
        for (int i = 0; i < N; i++) {
            cin >> w[i];
        }
        
        memset(memo, -1, sizeof(memo));
        cout << rec(0, N - 1) << endl;
    }
    return 0;
}