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 <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;

vector<vector<int>> dp;
vector<set<int>> edge;

bool match(int p, int q){
    return edge[p].count(q) > 0;
}


int search(const int l, const int r){
    if(l >= r) return 0;
    if(dp[l][r] > -1) return dp[l][r];
    int cand = search(l+1, r);
    for (auto &&u : edge[l]) {
        if(u > r) continue;
        int sc = search(l+1, u-1);
        if(sc == u - l - 1){
            cand = max(cand, sc + 2 + search(u+1, r));
        }
    } // end u
//    for (int i = l+1; i < r-1; ++i) {
//        cand = max(cand, search(l, i) + search(i+1, r));
//    } // end i

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


int main(){
    while(cin) {
        int n;
        cin >> n;
        if(n == 0) return 0;
        vector<int> w(n);
        for (auto &&i : w) {
            cin >> i;
        } // end i
        edge.clear();
        dp.clear();
        edge.resize(n);
        dp.resize(n, vector<int>(n, -1));

        for (int i = 0; i < n; ++i) {
            int p = w[i];
            for (int j =i+1; j < n; j+=2) {
                int q = w[j];
                if(abs(p - q) <= 1){
                    edge[i].insert(j);
                }
            } // end j
        } // end i
        cout << search(0, n-1) << endl;

    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int n;
	bool **check;
	int *dp;
	while (cin >> n&&n != 0) {
		int *line;
		dp = new int[n + 1]{ 0 };
		line = new int[n];
		check = new bool*[n];
		for (int i = 0; i < n; ++i) {
			cin >> line[i];
			check[i] = new bool[n] {0};
		}
		for (int i = 0, nmax = n - 1; i < nmax; ++i) {
			if (abs(line[i] - line[i + 1]) < 2) {
				check[i][i + 1] = true;
			}
		}
		for (int i = 3, nmax = n; i < nmax; i += 2) {
			for (int j = 0, mmax = n - i; j < mmax; ++j) {
				if ((check[j][j + 1] && check[j + 2][j + i]) || (check[j][j + i - 2] && check[j + i - 1][j + i]) || (check[j + 1][j + i - 1] && abs(line[j + i] - line[j]) < 2)) {
					check[j][j + i] = true;
				}
			}
		}
		for (int i = 0; i < n; ++i) {
			dp[i + 1] = max(dp[i + 1], dp[i]);
			for (int j = i + 1; j < n; j += 2) {
				if (check[i][j]) {
					dp[j + 1] = max(dp[j + 1], dp[i] + j - i + 1);
				}
			}
		}
		cout << dp[n] << endl;
		delete[] dp;
		for (int i = 0; i < n; ++i) {
			delete[] check[i];
		}
		delete[] check;
		delete[] line;
	}
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する

void solve(int n) {
    vector<int> w(n);
    rep(i, 0, n) cin >> w[i];
    vector<vector<int> > dp(n, vector<int>(n, 0));
    for (int i = 0; i + 1 < n; i++) {
        if (abs(w[i + 1] - w[i]) <= 1) {
            dp[i][i + 1] = 2;
        } else {
            dp[i][i + 1] = 0;
        }
    }
    for (int d = 2; d < n; d++) {
        for (int i = 0; i + d < n; i++) {
            int j = i + d;
            if (abs(w[j] - w[i]) <= 1 && dp[i + 1][j - 1] == j - i - 1) {
                dp[i][j] = j - i + 1;
            } else {
                for (int k = i; k < j; k++) {
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
                }
            }

        }
    }
    cout << dp[0][n-1] << endl;
//    if (n % 2 == 0) {
//        cout << dp[0][n - 1] << endl;
//    } else {
//        int ans = max(dp[0][n-2],dp[1][n-1]);
//        cout << ans << endl;
//
//    }
//    for(int i = 0; i < n; i++){
//
//    }

}

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

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

    }


    return 0;
}