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

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define whole(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bit(n) (1LL<<(n))
// functions
template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), c.end());}
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20);}
} fast_io_;

int dp[330][330];
int w[330];

int calc(int l, int r){
  if(dp[l][r] >= 0)return dp[l][r];
  if(r == l + 1)return dp[l][r] = 0;
  if(r == l + 2){
    if(abs(w[l] - w[l+1]) <= 1)return dp[l][r] = 2;
  }
  int res = 0;
  for(int k = l; k < r - 1; k++){
    chmax(res, calc(l, k+1) + calc(k+1, r));
  }
  if(abs(w[l] - w[l+1]) <= 1)chmax(res, 1 + calc(l+2, r));
  if(abs(w[r-2] - w[r-1]) <= 1)chmax(res, 1 + calc(l, r-2));
  if(abs(w[l] - w[r-1]) <= 1 && calc(l+1, r-1) == r - l - 2)chmax(res, r - l);
  return dp[l][r] = res;
}

int main(int argc, char const* argv[])
{
  int n;
  while(true){
    cin >> n;
    if(n == 0)break;
    rep(i, n)cin >> w[i];
    rep(i, n + 1){
      rep(j, n + 1)dp[i][j] = -1;
    }
    cout << calc(0, n) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
#include <cmath>
#include <functional>

using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;

int n;

int main()
{
	while(cin >> n , n)
	{
		vector<int> w(n,0);
		for(int i = 0;i < n;i++) cin >> w[i];

		vector<vector<int>> dp(n + 1,vector<int>(n + 1,-1));

		//[l,r)
		function<int(int,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;
			//l [able] r ?
			if(abs(w[l] - w[r - 1]) <= 1 && rec(l + 1,r - 1) == r - l - 2)
			{
				res = r - l;
			}

			//[l any] [any r]
			for(int mid = l + 1;mid <= r - 1;mid++)
			{
				res = max(res , rec(l,mid) + rec(mid,r));
			}
			return dp[l][r] = res;
		};

		cout << rec(0,n) << endl;
	}
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

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

vector<vector<int>>memo(1000, vector<int>(1000,-1));

int solve(const int l,const int r, vector<int>&v) {
	if (memo[l][r] != -1)return memo[l][r];
	if (l == r)return memo[l][r] = 0;
	else if (l + 1 == r)return memo[l][r] = 0;
	else if (l + 2 == r) {
		if (ok(v[l],v[l+1]))return memo[l][r] = 2;
		else return memo[l][r] = 0;
	}
	else {
		int ans = 0;
		for (int k = l + 1; k <= r - 1; ++k) {
			ans = max(ans, solve(l, k, v) + solve(k, r, v));
		}
		if (ok(v[l], v[r - 1]) && solve(l + 1, r - 1, v) == (r - l - 2)) {
			ans = r - l;
		}
		return memo[l][r]=ans;
	}
	
}

int main() {
	while (1) {
		memo = vector<vector<int>>(1000, vector<int>(1000, -1));
		int N; cin >> N;
		vector<int>v(N);
		for (int i = 0; i < N; ++i)cin >> v[i];
		if (!N)break;
		int ans = solve(0, N, v);
		cout << ans << endl;
	}
	return 0;
}