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>

// 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(300);
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 <bits/stdc++.h>
 
using namespace std;
using ll = long long;
 
#define rep(i, a) for (int i = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int i = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int i = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define RS resize
#define CINV(v, N) do {\
	v.RS(N);\
	rep(i, N) cin >> v[i];\
} while (0);
#define RCINV(v, N) do {\
	v.RS(N);\
	rrep(i, N) cin >> v[i];\
} while (0);
 
#define MOD 1000000007
bool init();
int solve();

signed main()
{
    while (init()) {
    	cout << solve() << endl;
	}
}

int N;
vector<int> w;

bool init()
{
	cin >> N;
	if (N == 0) return false;
	w.RS(N);
	rep(i, N) cin >> w[i];
	return true;
}


int solve()
{
	vector<vector<bool> > dp;
	vector<ll> imos;
	dp.RS(N, vector<bool>(N, false));
	imos.RS(N+1);
	for (int i = 0; i + 1 < N; i++) {
		if (abs(w[i] - w[i+1]) <= 1) {
			dp[i][i+1] = true;
			imos[i]++;
			imos[i+2]--;
		}
	}
	for (int l = 4; l <= N; l += 2) {
		for (int i = 0; i + l - 1 < N; i++) {
			int j = i + l - 1;
			if ((abs(w[i] - w[j]) <= 1 && dp[i+1][j-1]) || (dp[i][j-2] && dp[j-1][j])) {
				dp[i][j] = true;
				imos[i]++;
				imos[j+1]--;
			}
		}
	}
	int res = 0;
	int cnt = 0;
	rep(i, N) {
		if (imos[i] > 0) {
			cnt++;
		} else {
			res += cnt - (cnt % 2);
			cnt = 0;
		}
		imos[i+1] += imos[i];
	}
	res += cnt - (cnt % 2);
	return res;
}

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

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 301
int n;

vector<int> w(MAXN);
int dp[MAXN][MAXN];

bool isin(int i, int j){
  return 0<=i && j<n;
}

bool bashable(int i, int j){
  return abs(w[i]-w[j])<=1;
}

int solve(){
  rep(i, n-1)dp[i][i+1] = bashable(i, i+1) ? 2 : 0;
  for(int len=4; len<=n; len++){
    rep(from, n-len+1){
      int to = from+len-1;

      int inner = from+1<to-1 ? dp[from+1][to-1] : 0;
      if(inner==len-2 && bashable(from, to))inner += 2;
      dp[from][to] = inner;

      for(int m=from; m<=to;m++){
        dp[from][to] = max(dp[from][to], dp[from][m]+dp[m+1][to]);
      }

      // rep(i, n){rep(j, n)cout<<dp[i][j]<<' ';cout<<endl;}cout<<endl;

    }
  }

  return dp[0][n-1];
}

int main(){
  while(cin>>n){
    if(!n) break;

    rep(i, MAXN)w[i]=0;
    rep(i, MAXN)rep(j, MAXN)dp[i][j]=0;
    rep(i, n)cin>>w[i];
    int result = solve();
    cout<<result<<endl;

// rep(i, n){rep(j, n)cout<<dp[i][j]<<' ';cout<<endl;}cout<<endl;

  }

  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int dp[300][300];//dp[l][r] [l,r]でだるまを落としたときの落とした個数

int main()
{
	while(1)
	{
		int n;
		cin>>n;
		if(n==0)
			break;
		
		int da[300];
		for(int i=0;i<n;i++)
			cin>>da[i];
		fill(dp[0],dp[300],0);
		for(int i=1;i<n;i++)
		{
			int dif=da[i]-da[i-1];
			if(dif<2 && dif>-2)
				dp[i-1][i]=2;
			else
				dp[i-1][i]=0;
		}
		//ここからdp
		for(int k=2;k<n;k++)
		{
			for(int i=0;i<n-k;i++)
			{
				//分割して調べる
				for(int j=i;j<i+k-1;j++)
					dp[i][i+k]=max(dp[i][i+k],dp[i][j]+dp[j+1][i+k]);
				//真ん中が全部消える場合も調べる
				int dif=da[i]-da[i+k];
				if(dp[i+1][i+k-1]==k-1 && dif<2 && dif>-2)
					dp[i][i+k]=k+1;
			}
		}
		cout<<dp[0][n-1]<<endl;
	}
	return 0;
}