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;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<(int)(n);i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int memo[302][302];
int w[302];

int rec(int i,int j){
    if(i>=j) return 0;
    if(i+1==j){
        if(abs(w[i]-w[j]) <= 1) return 2;
        else return 0;
    }
    if(memo[i][j]) return memo[i][j];
    int res=0;

    if(abs(w[i]-w[j])<=1 and rec(i+1,j-1)==j-1-i){
        return memo[i][j]=j-i+1;
    }

    for(int k=i;k<j;k++){
        int L=rec(i,k);
        int R=rec(k+1,j);
        res=max(res,L+R);
    }
    return memo[i][j]=res;
}

void solve(int N){
    rep(i,302) rep(j,302) memo[i][j]=0;
    rep(i,N) cin >> w[i];
    cout << rec(0,N-1) << endl;
}

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

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 DEBUG(C) cerr << #C << " = " << C << endl

using namespace std;

const int MAX = 333;
int w[MAX];
int dp[MAX][MAX];

bool solve() {
  int n;
  scanf("%d", &n);
  if (n == 0) {
    return false;
  }
  memset(w, 0, sizeof(w));
  for (int i = 0; i < n; i++) {
    scanf("%d", w + i);
  }
  memset(dp, 0, sizeof(dp));
  
  for (int len = 2; len <= n; len++) {
    for (int l = 0; l <= n - len; l++) {
      const int r = l + len;
      for (int m = l; m <= r; m++) {
        dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
      }
      if (abs(w[l] - w[r - 1]) <= 1 && dp[l + 1][r - 1] == (r - 1) - (l + 1)) {
        dp[l][r] = r - l;
      }
    }
  }
  
  printf("%d\n", dp[0][n]);
  return true;
}

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

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
#define ALL(x)    (x).begin(),(x).end()
#define UNIQ(c)   (c).erase(unique(ALL((c))), end((c)))
#define mp        make_pair
#define eb        emplace_back

int n;
int w[330];
int dp[330][330];


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

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

int DFS(vector<int> &w, vector<bool> f, int n, unordered_set<vector<bool>> &s) {
  int max = 0;
  for (int i = 0; i < n; i++) {
    if (f[i]) {
      for (int j = i + 1; j < n; j++) {
	if (f[j]) {
	  if (abs(w[i] - w[j]) <= 1) {
	    f[i] = f[j] = false;
	    if (s.find(f) == s.end()) {
	      s.emplace(f);
	      int ret = DFS(w, f, n, s) + 2;
	      if (ret > max) {
		max = ret;
	      }
	    }
	    f[i] = f[j] = true;
	  }
	  break;
	}
      }
    }
  }
  return max;
}

int main() {
  vector<int> w(300);

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

    if (n == 0) {
      break;
    }

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

    unordered_set<vector<bool>> s;
    cout << DFS(w, f, n, s) << endl;
  }

  return 0;
}