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 MKP make_pair
typedef pair<int, int> pii;

constexpr int MAX_N = 305;
constexpr int INF = 1LL << 60;

int n, mem[MAX_N][MAX_N], w[MAX_N];

int solve(int l, int r) {
    if (r - l <= 1) {
        return 0;
    }
    if (mem[l][r] != -1) {
        return mem[l][r];
    }

    int res = 0;
    for (int i = l + 1; i < r; ++i) {
        res = max(res, solve(i, r));
    }
    for (int i = r - 1; l + 1 <= i; --i) {
        res = max(res, solve(l, i));
    }

    if (abs(w[l] - w[l + 1]) <= 1) {
        res = max(res, solve(l + 2, r) + 2);
    }
    if (abs(w[r - 1] - w[r - 2]) <= 1) {
        res = max(res, solve(l, r - 2) + 2);
    }
    if (abs(w[l] - w[r - 1]) <= 1) {
        int tmp = solve(l + 1, r - 1);
        if (tmp == r - l - 2) {
            res = max(res, tmp + 2);
        }
    }

    return mem[l][r] = res;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while(1) {
        cin >> n;
        if (n == 0) {
            return 0;
        }
        memset(mem[0], -1, sizeof(mem));

        for (int i = 0; i < n; ++i) {
            cin >> w[i];
        }

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

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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;




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

        vector<int> w(n);
        REP(i,n) cin >> w[i];

        vector<vector<int>> dp(n,vector<int>(n,0));

        for(int width=2; width<=n; width++){//もらうDP
            FOR(left,0,n-width+1){
                int right = left+width-1;
                if(width%2==0 && dp[left+1][right-1]==width-2){//内側の区間が適切に処理でき、
                    if(abs(w[left] - w[right])<=1){//端の差が1以下なら
                        dp[left][right] = width;
                    }
                }

                for(int mid=left; mid<right; mid++){
                    dp[left][right] = max(dp[left][right], dp[left][mid] + dp[mid+1][right]);
                }
                
            }
        }

        cout << dp[0][n-1] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

using lint = long long;
using ldouble = long double;

vector<int> W;
vector<vector<int>> dp;

bool rec(int L, int R) {
    auto& ret = dp[L][R];
    if (ret >= 0) return ret;

    if (L == R) return ret = 1;
    if ((R - L) & 1) return ret = 0;

    for (int i = L + 1; i < R; i += 2) {
        if (abs(W[L] - W[i]) > 1) continue;
        if (rec(L + 1, i) && rec(i + 1, R)) return ret = 1;
    }
    return ret = 0;
}

bool solve() {
    int N;
    cin >> N;
    if (N == 0) return false;

    W.resize(N);
    for (auto& w : W) cin >> w;

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

    vector<int> ans(N + 1, 0);
    for (int i = 0; i < N; ++i) {
        ans[i + 1] = max(ans[i], ans[i + 1]);
        for (int len = 2; i + len <= N; ++len) {
            if (rec(i, i + len)) {
                ans[i + len] = max(ans[i + len], ans[i] + len);
            }
        }
    }

    cout << ans[N] << endl;
    return true;
}

int main() {
    while (solve()) {}
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<stack>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<deque>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int main()
{
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

ll w[310];
ll dp[310][310];
vector<ll> v;

void solve(){

    while(true){
    ll n;
    cin >> n;
    if(n==0) break;
    rep(i,n){
        cin >> w[i];
    }
    rep(i,310){
        rep(j,310){
            dp[i][j]=0;
        }
    }

    for(ll i=1; i<n; i++){
        for(ll j=0; j<n-i; j++){
            ll x=j;
            ll y=i+j;
            if(dp[x+1][y-1]==y-x-1&&abs(w[x]-w[y])<=1){
                dp[x][y]=y-x+1;
            }
            else{
                ll ans = 0;
                rep(k,y-x){
                    ans=max(ans, dp[x][x+k]+dp[x+k+1][y]);
                }
                dp[x][y]=ans;
            }
        }
    }

    /*
    rep(i,n){
        rep(j,n){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }
    */
    v.emplace_back(dp[0][n-1]);


    }

    for(auto e:v){
        cout << e << endl;
    }

}


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