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>
using namespace std;

#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()

template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }

class Solver {
  public:
    vector<int> W;
    vector<vector<int>> DP;

    // [i, j]
    bool removable(int i, int j) {
        if(i > j) return true;
        if(i == j) return DP[i][j] = false;
        if(DP[i][j] >= 0) return DP[i][j];

        bool ok = false;
        repeat(k, i + 1, j + 1) { // [i+1, j]
            ok |= abs(W[i] - W[k]) <= 1 and removable(i + 1, k - 1) and removable(k + 1, j);
        }
        return DP[i][j] = ok;
    }
    
    bool solve() {
        int N; cin >> N;
        if(not N) return false;
        W.resize(N); cin >> W;

        DP.resize(N, vector<int>(N, -1));
        rep(i, N) rep(j, N) removable(i, j);

        vector<int> max_removable(N + 1, 0);

        rep(i, N) {
            set_max(max_removable[i + 1], max_removable[i]);
            repeat(j, i + 1, N) {                
                if(removable(i, j)) set_max(max_removable[j + 1], max_removable[i] + j - i + 1);
            }
        }
        cout << max_removable.back() << endl;
        return true;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        Solver s;
        if(not s.solve()) break;
    }
    return 0;
}

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

#define vi vector<int>
#define vl vector<long long>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORD(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define INF 1e18
#define MOD 1e9+7

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

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

const int MAX_N = 310;
vector< vector<int> > dp;
int w[MAX_N];

//dp[l][r]:[l, r)までのブロックで取り除ける最大の数
//dp[l][l+2] = 2 ? w[l]-w[l+1] <= 1 else 0

int rec(int l, int r) {
    if (dp[l][r] >= 0) return dp[l][r];
    if (r-l<=1) return dp[l][r] = 0;
    if (abs(w[r-1]-w[l]) <= 1 && rec(l+1, r-1) == r-l-2) chmax(dp[l][r], r-l);
    FOR(i, l+1, r) {
        chmax(dp[l][r], rec(l, i)+rec(i, r));
    }
    return dp[l][r];
}

int main() {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        REP(i, n) {
          cin >> w[i];
        }
        dp.assign(n+1, vector<int>(n+1, -1));
        cout << rec(0, n) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

int main(){
	int n;
	while(cin >> n && n!=0){
		vector<int> v(n);
		for(int i=0;i<n;i++){
			cin >> v[i];
		}
		vector<vector<int> > dp(n,vector<int>(n,0));
		int ans=0;
		int mx=0;
		for(int i=2;i<=n;i++){
			for(int j=0;j<=n-i;j++){
				if(i==2){
					if(v[j]<=v[j+1]+1&&v[j]>=v[j+1]-1){
						dp[j][j+1] = 2;
					}
				}else{
					if(dp[j+1][j+i-2]==i-2&&v[j]<=v[j+i-1]+1&&v[j]>=v[j+i-1]-1){
						dp[j][j+i-1]=i;
					}
					mx = dp[j][j+i-1];
					for(int k=0;k<=i-2;k++){
						mx = max(mx,dp[j][j+k]+dp[j+k+1][j+i-1]);
						dp[j][j+i-1] = mx;
					}
				}
			}
		}

		cout << dp[0][n-1] << endl;
	}
	return 0;
}

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

using namespace std;
typedef long long ll;

int n;
vector<int> w;
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(w[l] - w[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;
};

int main() {
    vector<int> ans(0);
    while (true) {
        cin >> n;
        if (n == 0) break;
        w.resize(n);
        for (int i = 0; i < n; i++) {
            cin >> w[i];
        }
        dp = vector<vector<int> >(n+2, vector<int>(n+2, -1));
        ans.push_back(rec(0, n));
    }

    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <string>
#define REP(i,a,b) for (int i = a; i < (b); i++)
#define sz(t) int(t.size())
#define INF 1000000000
const int DIV = 1000000000 + 7;
using namespace std;
typedef long long ll;

#define MOD 10007

int main(void) {
    int n;
    for (;;) {
        cin >> n;
        if (n == 0) {
            break;
        }
        vector<int> W(n + 1);
        REP(i, 0, n) {
            cin >> W[i];
        }
        vector<vector<int> > dp(n + 1, vector<int>(n + 1, 0));
        REP(w, 2, n + 1) {
            REP(l, 0, n) {
                int r = l + w;
                if (r > n) {
                    continue;
                }
                if (dp[l + 1][r - 1] == w - 2 && abs(W[l] - W[r - 1]) <= 1) {
                    dp[l][r] = dp[l + 1][r - 1] + 2;
                }
                REP(mid, l, r + 1) {
                    dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
                }
            }
        }
        cout << dp[0][n] << endl;
    }
}