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;
typedef long long ll;

int main() {
	while (1) {
		int n; cin >> n;
		if (n == 0) return 0;
		vector<int> w(n);
		for (int i = 0; i < n; ++i) cin >> w[i];
		vector<vector<int> > dp(n, vector<int>(n, 0));
		for (int i = 0; i < n-1; ++i) {
			if (abs(w[i] - w[i+1]) <= 1) dp[i][i+1] = 2;
		}
		for (int d = 2; d < n; ++d) {
			for (int i = 0; i < n-d; ++i) {
				if (dp[i+1][i+d-1] == d-1 and abs(w[i] - w[i+d]) <= 1) dp[i][i+d] = d+1;
				else {
					for (int j = i; j < i+d; ++j) {
						dp[i][i+d] = max(dp[i][i+d], dp[i][j] + dp[j+1][i+d]);
					}
				}
			}
		}
		cout << dp[0][n-1] << "\n";
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#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(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
int x[1000];
bool dp[1000][1000];

int main() {
	while (cin >> n&&n) {
		rep(i, 1000)rep(j, 1000) {
			dp[i][j] = 0;
		}
		rep(i, n)cin >> x[i];
		for (int i = 1; i < n; i += 2) {
			for (int j = 0; j + i < n; j++) {
				int lb = j, ub = j + i;
				if (abs(x[lb] - x[ub]) <= 1) {
					if (i == 1)dp[lb][ub] = 1;
					else dp[lb][ub] |= dp[lb + 1][ub - 1];
				}
				FOR(mid, lb, ub) {
					dp[lb][ub] |= dp[lb][mid] && dp[mid + 1][ub];
				}
			}
		}
		int ans = 0;
		rep(i, n) {
			int num = 0;
			int pos = i;
			for (int j = i + 1; j < n; j += 2) {
				if (dp[i][j]) {
					num = max(num, j - i + 1);
					pos = j - 1;
				}
			}
			ans += num;
			i = pos;
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    int n; cin >> n;
    if(n == 0) break;
    vector<int> w(n);
    for(int i=0;i<n;++i) {
      cin >> w[i];
    }
    vector<vector<int>> dp(n, vector<int>(n, 0));
    for(int i=0;i<n-1;++i) {
      if(abs(w[i] - w[i+1]) <= 1) {
        dp[i][i+1] = 2;
      }
    }
    for(int i=2;i<n;++i) {
      for(int l=0;l<n-i;++l) {
        int r = l + i;
        if(dp[l+1][r-1] == r - l - 1 && abs(w[l] - w[r]) <= 1) {
          dp[l][r] = r - l + 1;
        }
        for(int k=l;k<=r-1;++k) {
          dp[l][r] = max(dp[l][r], dp[l][k] + dp[k+1][r]);
        }
      }
    }
    cout << dp[0][n-1] << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define repr(i,n) for(int i=(int)(n-1);i>=0;i--)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout << #x" = " << x << endl
#define print(x) cout << x << endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX/3
#define MAX_N 1000000001


int main(){
  cin.sync_with_stdio(false);
  int n;
  while(cin >> n, n) {
    vector<int> w(n);
    rep(i, n) cin >> w[i];

    int dp[n][n] = {};
    rep(i, n - 1) if (abs(w[i] - w[i + 1]) < 2) dp[i + 1][i] = 2;

    repl(i, 2, n) {
      rep(j, n - i) {
        rep(k, i) {
          maxch(dp[j + i][j], dp[j + k][j] + dp[j + i][j + k + 1]);
        }
        if (dp[j + i - 1][j + 1] == i - 1) maxch(dp[j + i][j], dp[j + i - 1][j + 1] + ((abs(w[j] - w[j + i]) < 2) ? 2 : 0));
      }
    }
    cout << dp[n - 1][0] << endl;
  }

  return 0;
}