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 <algorithm>
#include <vector>
#include <queue>
#include <string>

using ll = long long;

using namespace std;

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        int w[n];
        for (auto &wi:w) cin >> wi;
        bool erase[n][n];
        fill(erase[0], erase[n], false);
        for (int d=1; d<n; d++) {
            for (int i=0; i+d<n; i++) {
                if ((d == 1 || erase[i+1][i+d-1]) && abs(w[i] - w[i+d]) <= 1)
                    erase[i][i+d] = true;
                for (int j=i+1; j+1<i+d; j++) {
                    if (erase[i][j] && erase[j+1][i+d]) erase[i][i+d] = true;
                }
            }
        }
        int dp[n+1];
        fill(dp, dp+n+1, 0);
        for (int i=0; i<n; i++) {
            for (int j=i+1; j<n; j++) {
                if (erase[i][j]) {
                    dp[j+1] = max(dp[j+1], dp[i] + j - i + 1);
                }
            }
            dp[i+1] = max(dp[i+1], dp[i]);
        }
        cout << dp[n] << '\n';
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define bit(n) (1LL<<(n))
#define pcnt(x) __builtin_popcountll(x)
#define PB push_back
#define MP make_pair
#define endl "\n"
using Edge = pair<int,int>;
using Graph = vector<vector<Edge>>;
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; }
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

bool cantake[310][310];
int dp[310][310];

int main() {
  while(1){
    int n; cin >> n;
    if(n==0) break;
    int w[n+2];
    MEMSET(cantake,false);
    MEMSET(dp,0);
    w[0] = 1001001001;
    w[n+1] = 1001001001;
    FOR(i,1,n) cin >> w[i];

    FOR(wid,2,n+1){
      FOR(i,1,n){
        int j = i+wid-1;
        if(j>=n+1) continue;

        if(dp[i+1][j-1]==wid-2 && abs(w[i]-w[j])<=1) dp[i][j]=wid;
        FOR(k,i,j){
          chmax(dp[i][j], dp[i][k] + dp[k+1][j]);
        }
      }
    }
    cout << dp[1][n] << "\n";
  }

}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,w[300];
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>w[i];
    int dp[300][300]={};
    for(int i=1;i<n;i++)
      for(int j=0;j+i<n;j++){
	for(int k=j;k<j+i;k++)
	  dp[j][j+i]=max(dp[j][j+i],dp[j][k]+dp[k+1][j+i]);
	if(dp[j+1][j+i-1]==i-1&&abs(w[j]-w[j+i])<2)dp[j][j+i]=i+1;
      }
    cout<<dp[0][n-1]<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define N 305
using namespace std;
int n,w[N],dp[N][N];

int main(){
  while(1){
    cin>>n;
    if(!n)break;
    for(int i=0;i<n;i++)
      cin>>w[i];
    memset(dp,0,sizeof(dp));
    int ans=0;
    for(int i=1;i<=n;i+=2){
      for(int j=0;j<n-i;j++){
	dp[j][j+i]=dp[j+1][j+i-1];
	for(int k=j+1;k<=j+i;k+=2)
	  dp[j][j+i]=max(dp[j][j+i],dp[j][k]+dp[k+1][j+i]);
	if(dp[j+1][j+i-1]==i-1&&abs(w[j]-w[j+i])<=1)
	  dp[j][j+i]=max(dp[j][j+i],dp[j+1][j+i-1]+2);
	ans=max(ans,dp[j][j+i]);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i, n, N) for(ll i=(n); i<(N); i++)
#define RREP(i, n, N) for(ll i=(N-1); i>=(n); i--)
#define LREP(lst,itr) for(auto itr = lst.begin(); itr != lst.end(); ++itr)
#define SPACEREP(lst,itr) for(auto itr=lst.begin(); itr!=lst.end();++itr){cout<<*itr<<(++itr!=anslist.end()?" ":"\n");itr--;}
#define CK(n, a, b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define MCP(a, b) memcpy(b,a,sizeof(b))
#define P(s) cout<<(s)<<endl
#define P2(a, b) cout<<(a)<<" "<<(b)<<endl
#define V2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int dp[310][310];

int main(){
    int n;
    while(cin >> n && n){
        vector<int> line(n);
        REP(i,0,n) cin >> line[i];
        REP(i,0,310){
            REP(j,0,310){
                dp[i][j]=0;
            }
        }

        REP(len,2,n+1){
            REP(left,0,n){
                int right = left + len;
                if(right > n)continue;
                if(dp[left+1][right-1]==len-2 && abs(line[left]-line[right-1])<=1){
                    dp[left][right]=len;
                }

                REP(mid,left+1,right){
                    dp[left][right]=max(dp[left][right],dp[left][mid]+dp[mid][right]);
                }
            }
        }
        cout << dp[0][n] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <queue>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> E;

llint n;
llint w[305];
bool dp[305][305];
llint dp2[305];

int main(void)
{
	while(1){
		cin >> n;
		if(n == 0) break;
		for(int i = 1; i <= n; i++) cin >> w[i];
		
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				dp[i][j] = false;
			}
		}
		for(int i = 1; i <= n-1; i++){
			dp[i][i+1] = (abs(w[i]-w[i+1]) <= 1);
		}
		for(int len = 4; len <= n; len += 2){
			for(int l = 1; l+len-1 <= n; l++){
				llint r = l+len-1;
				for(int k = l+1; k <= r; k++) dp[l][r] |= dp[l][k-1] && dp[k][r];
				if(abs(w[l]-w[r]) <= 1) dp[l][r] |= dp[l+1][r-1];
			}
		}
		
		for(int i = 0; i <= n; i++) dp2[i] = -inf;
		dp2[0] = 0;
		for(int i = 0; i < n; i++){
			dp2[i+1] = max(dp2[i+1], dp2[i]);
			for(int j = i+1; j <= n; j++){
				if(dp[i+1][j]) dp2[j] = max(dp2[j], dp2[i]+(j-i));
			}
		}
		cout << dp2[n] << endl;
	}
	
	return 0;
}