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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

ll n;
vector<ll> a;
ll dp[350][350];

ll dfs(ll left, ll right) {
	if (dp[left][right] != -1) return dp[left][right];
	if (left == right) return 0;
	if (left + 1 == right) return 0;
	ll res = 0;
	for (ll i = left + 1; i < right; i++) {
		chmax(res, dfs(left, i) + dfs(i, right));
	}
	if (llabs(a[right - 1] - a[left]) <= 1 && dfs(left + 1, right - 1) == (right - left - 2)) {
		chmax(res, dfs(left + 1, right - 1) + 2);
	}
	return dp[left][right] = res;
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	while (1) {
		ll n; scanf("%lld", &n);
		if (n == 0) break;
		a.resize(n); for (ll i = 0; i < n; i++) scanf("%lld", &a[i]);
		for (ll i = 0; i <= n; i++) for (ll j = 0; j <= n; j++) dp[i][j] = -1;
		printf("%lld\n", dfs(0, n));
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int a[301],n,dp[301][301];
int main(){
  while(cin>>n,n){
    int ans=0;
    r(i,n)cin>>a[i];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i+=2)
      for(int j=0;j<n-i;j++){
        for(int k=1;k<i;k+=2)dp[j][j+i]=max(dp[j][j+i],dp[j][j+k]+dp[j+k+1][j+i]);
        if(abs(a[j]-a[j+i])<2&&dp[j+1][j+i-1]==i-1)dp[j][j+i]=i+1;
      }
    r(i,n)r(j,n)ans=max(ans,dp[i][j]);
    cout<<ans<<endl;
  }
}

C++: #pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

void chmax(ll& x, ll y) {
    if (x < y) x = y;
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

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

        vll w(n); REP(i, n) cin >> w[i];

        vector<vll> dp(n + 1, vll(n + 1, 0));

        FOR(l, 2, n + 1) {
            REP(i, n) {
                int j = i + l - 1;
                if (j >= n) break;

                if (dp[i+1][j-1] == l - 2 && abs(w[i] - w[j]) <= 1) {
                    chmax(dp[i][j], l);
                } else {
                    FOR(k, i, j) {
                        chmax(dp[i][j], dp[i][k] + dp[k+1][j]);
                    }
                }
            }
        }

        print(dp[0][n-1]);
    }
}

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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ull << (b))

using namespace std;
using i32 = int;
using i64 = long long;
using f64 = double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf64 = vector<f64>;
using vstr = vector<string>;

template<typename T, typename S> void amax(T &x, S y) { if (x < y) x = y; }
template<typename T, typename S> void amin(T &x, S y) { if (y < x) x = y; }

int W[300], dp[300][300];

int rec(int l, int r) {
  if (l + 1 >= r) return 0;
  if (~dp[l][r]) return dp[l][r];
  if (abs(W[l] - W[r - 1]) <= 1 && rec(l + 1, r - 1) == r - l - 2) {
    return dp[l][r] = r - l;
  }
  int ret = 0;
  repi(m, l + 1, r) {
    ret = max(ret, rec(l, m) + rec(m, r));
  }
  return dp[l][r] = ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  while (true) {
    int N;
    cin >> N;
    if (!N) break;
    rep(i, N) cin >> W[i];
    memset(dp, -1, sizeof(dp));
    cout << rec(0, N) << endl;
  }
  return 0;
}