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 int long long
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef unsigned long long ul;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18 + 1;
const ll dx[] = {1, -1, 0, 0};
const ll dy[] = {0, 0, 1, -1};

ll n;
VL w;

ll secdp(ll n) {
    vector<VL> dp(n, VL(n, 0));
    REP(i, n - 1) if(abs(w[i] - w[i + 1]) <= 1) { dp[i][i + 1] = 2; }
    FOR(i, 2, n) REP(l, n - i) {
        ll r = l + i;
        if(abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == i - 1) {
            dp[l][r] = i + 1;
        }
        FOR(k, l + 1, r + 1) {
            dp[l][r] = max(dp[l][r], dp[l][k - 1] + dp[k][r]);
        }
    }
    return dp[0][n - 1];
}

signed main() {
    while(cin >> n) {
        if(n == 0) break;
        REP(i, n) w.emplace_back(IN());
        cout << secdp(n) << "\n";
        w.clear();
    }
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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; }

int n, a[300], dp[300][300];

int rec(int l, int r){
    if(l >= r) return 0;
    if(~dp[l][r]) return dp[l][r];
    int ret = 0;
    if(abs(a[l]-a[r]) <= 1 && rec(l+1, r-1) == r-l-1) ret = r-l+1;
    for(int k = l; k < r; k++){
        chmax(ret, rec(l, k) + rec(k+1, r));
    }
    return dp[l][r] = ret;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
    while(true){
        cin >> n;
        if(n == 0) break;
        rep(i,n) cin >> a[i];
        memset(dp, -1, sizeof(dp));
        cout << rec(0, n-1) << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
 
using namespace std;
 
int N, W[300], dp[300][300];
 
int rec(int l, int r)
{
  if(l >= r) return (0);
  if(~dp[l][r]) return (dp[l][r]);
  int ret = 0;
  if(abs(W[l] - W[r]) <= 1 && rec(l + 1, r - 1) == r - l - 1) ret = r - l + 1;
  else for(int m = l; m < r; m++) ret = max(ret, rec(l, m) + rec(m + 1, r));
  return (dp[l][r] = ret);
}
 
int main()
{
  while(scanf("%d", &N), N) {
    for(int i = 0; i < N; i++) {
      scanf("%d", &W[i]);
    }
    memset(dp, -1, sizeof(dp));
    printf("%d\n", rec(0, N - 1));
  }
}

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=1;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;
int N;
int W[305];
int dp[305][305];
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0){break;}
		for(int i=0;i<N;i++){
			scanf("%d",&W[i]);
		}
		for(int i=0;i<305;i++){
			for(int j=0;j<305;j++){
				dp[i][j]=0;
			}
		}
		for(int i=0;i<=N-2;i++){
			if(abs(W[i]-W[i+1])<=1){dp[i][2]=2;}
		}
		for(int j=3;j<=N-1;j++){
			for(int i=0;i+j<=N;i++){
				//[i,i+j)
				for(int k=0;k<j;k++){
					dp[i][j]=max(dp[i][k]+dp[i+k][j-k],dp[i][j]);
				}
				if(abs(W[i]-W[i+j-1])<=1){
					dp[i][j]=max(dp[i][j],dp[i+1][j-2]+2);
				}
			}
		}
		/*
		for(int j=0;j<12;j++){
			for(int i=0;i+j<=12;i++){
				printf("%d ",dp[i][j]);
			}
			printf("\n");
		}*/
		/*
		for(int i=150;i>=0;i-=2){
		printf("%d\n",dp[i][299-i]);
		}*/
		if(N%2==0){printf("%d\n",dp[0][N-1]);}
		else {printf("%d\n",max(dp[0][N-2],dp[1][N-2]));}
	}
	return 0;
}