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<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (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 i = 0;i < (n);i++)cerr << v[i] << ' ';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)

using namespace std;

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

int dp[310][310];
int n;
vector<int> node;

int dfs(int l,int r)
{
	if(l > r)return 0;
	if(l == r)return 0;
	if(dp[l][r] != -1)return dp[l][r];
	if(r - l == 1)return (abs(node[l]-node[r])<2)?2:0;
	
	int ret = 0;
	
	if((r-l)%2==1&&dfs(l+1,r-1)==r-l-1&&(abs(node[l]-node[r])<2))
	{
		return r-l+1;
	}
	
	for(int i=1;i < r-l;i++)
	{
		ret = max(ret,dfs(l,l+i)+dfs(l+i+1,r));
	}
	
	return dp[l][r] = ret;
}

int main()
{
	while(cin >> n,n)
	{
		
		REP(i,310)REP(j,310)dp[i][j] = -1;
		node.clear();
		
		REP(i,n)
		{
			int tmp;cin >> tmp;
			node.PB(tmp);
		}
		
		cout << dfs(0,n-1) << endl;
		
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N;

void chmax(int &a,int b){
    if(a<b) a=b;
}

void solve(){
    vector<int> w(N);
    for(int i=0;i<N;i++) cin>>w[i];

    int dp[303][303]={};
    for(int i=0;i<N-1;i++){
        if(abs(w[i]-w[i+1])<=1) dp[i][i+1]=2;
    }

    for(int k=3;k<=N;k++){
        for(int i=0;i+k-1<N;i++){
            int lef=i;
            int rig=i+k-1;
            for(int j=lef;j<rig;j++){
                chmax(dp[lef][rig],dp[lef][j]+dp[j+1][rig]);
            }
            if(abs(w[lef]-w[rig])<=1&&dp[lef+1][rig-1]==(k-2)) dp[lef][rig]=k;
        }
    }

    cout<<dp[0][N-1]<<endl;
}

int main(){
  while(1){
      cin>>N;
      if(N==0) break;
      solve();
  }

  return 0;
}

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

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp make_pair

// Loop
#define FOR(i,a,b) for(auto i=(a);i<(b);++i)
#define RFOR(i,a,b) for(auto i=(a);i>=(b);--i)

#define REP(i,a) for(long i=0;i<(a);++i)
#define RREP(i,a) for(long i=(a);i>=0;--i)

#define EACH(i,a) for(auto (i)=(a).begin(),_END=(a).end();i!=_END;++i)
#define REACH(i,a) for(auto (i)=(a).rbegin(),_END=(a).rend();i!=_END;++i)

//Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a,x) ((a).find(x)!=(a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a) std::sort((a).begin(), a.end()), a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

//Setting
#define OPT std::cin.tie(0);std::ios::sync_with_stdio(false);

//debug message
bool debug = true;
#define MSG(s)   if(debug){std::cout << s << std::endl;}
#define DEBUG(x) if(debug){std::cout << "debug(" << #x << "): " << x << std::endl;}

//alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int>  VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector< VC > VC2;
typedef std::vector< VI > VI2;
typedef std::vector< VL > VL2;
typedef std::vector< VLL > VLL2;

typedef std::pair<int,int> PII;


int n;
VI w(301);
VI2 memo(301, VI(301));

int dp(int l, int r) {
    if(l == r) return 0;
    if(r-l == 1) return (std::abs(w[l]-w[r]) <= 1) ? 2 : 0;
    if(memo[l][r] != -1) return memo[l][r];

    int ans = 0;
    if(std::abs(w[l]-w[r]) <= 1) {
        int res = dp(l+1, r+1);
        if(res == r-l-1) {
            ans = 2 + res;
        }
    }
    for(int i=l; i<r; ++i) {
        ans = std::max(ans, dp(l, i) + dp(i+1, r));
    }
    return memo[l][r] = ans;
}

int main() {
    while(std::cin >> n, n != 0) {
        REP(i, n) {
            std::cin >> w[i];
        }
        memo.assign(301, VI(301, -1));
        std::cout << dp(0, n-1) << std::endl;
    }
}

C++: #include <iostream>
using namespace std;

int w[400];
int memo[400][400];

int max(int i, int j){
	int max = 0;
	for(int k = i ; k < j ; k++){
		int a = memo[i][k] + memo[k+1][j];
		if(a > max){
			max = a;
		}
	}
//	if((j - i) % 2 == 0){
		if(w[i] - w[j] <= 1 && w[j] - w[i] <= 1 && memo[i+1][j-1] == j - i - 1){
			max = j - i + 1;
//			max += 2;
// 		}
	}
	return max;
}

int main() {
	int n;
	while(true){
		cin >> n;

		if(n == 0) break;
		for(int k = 0 ; k < n ; k++){
			cin >> w[k];
//			cout << "w[" << k << "] = " << w[k] << "\n";
		}

		for(int k = 0 ; k < n - 1; k++){
			memo[k][k] = 0;
//			cout << "memo[" << k << "][" << k << "] = " << memo[k][k] << "\n";
		}

		for(int k = 0 ; k < n - 1; k++){
			memo[k][k+1] = (w[k] - w[k+1] <= 1 && w[k+1] - w[k] <= 1) ? 2 : 0;
//			cout << "memo[" << k << "][" << k + 1 << "] = " << memo[k][k + 1] << "\n";
		}

		for(int b = 2 ; b < n ; b++){ //???
			for(int k = 0 ; k < n - b ; k++){
				memo[k][k + b] = max(k, k + b);
//				cout << "memo[" << k << "][" << k + b << "] = " << memo[k][k + b] << "\n";
			}
		}
//		cout << "memo[" << 0 << "][" << n - 1 << "] = " << memo[0][n-1] << "\n";
		cout << memo[0][n-1] << "\n";
	}
		return 0;
}