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>
#define REP(i,n) for (long long i=0;i<(n);i++)
#define FOR(i,a,b) for (long long i=(a);i<(b);i++)
#define RREP(i,n) for(long long i=n;i>=0;i--)
#define RFOR(i,a,b) for(long long i=(a);i>(b);i--)
#define dump1d_arr(array) REP(i,array.size()) cerr << #array << "[" << (i) << "] ==> " << (array[i]) << endl
#define dump2d_arr(array) REP(i,array.size()) REP(j,array[i].size()) cerr << #array << "[" << (i) << "]" << "[" << (j) << "] ==> " << (array[i][j]) << endl
#define dump(x)  cerr << #x << " => " << (x) << endl
#define dumpP(p) cerr << "( " << p.first << " , " << p.second << " )" << ends
#define SORT(c) sort((c).begin(),(c).end())
#define MIN(vec) *min_element(vec.begin(), vec.end())
#define MAX(vec) *max_element(vec.begin(), vec.end())
#define UNIQ(vec) vec.erase(unique(vec.begin(), vec.end()),vec.end()) //ソートの必要あり
#define IN(n,m)  (!(m.find(n) == m.end()))
#define ENUM(m) for (auto itr = m.begin(); itr != m.end(); ++itr)
#define dump_MAP(m) for(auto itr = m.begin(); itr != m.end(); ++itr) { cerr << itr->first << " --> "  << itr->second << endl; }
#define FINDL(vec,x) (lower_bound(vec.begin(),vec.end(),x) - vec.begin())
#define FINDU(vec,x) (upper_bound(vec.begin(),vec.end(),x) - vec.begin())
#define ROUND(N) setprecision(N)
#define ROUND_PRINT(N,val) cout << fixed;cout << setprecision(N) << val << endl
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define INARR(h,w,x,y) (0 <= y && y < h && 0 <= x && x < w)
#define EQ(a,b) (abs(a - b) < 1e-10)
using namespace std;
constexpr int dx[4] = {0,1,0,-1};
constexpr int dy[4] = {1,0,-1,0};
constexpr long double pi = M_PI;
constexpr double eps = 1e-10;
constexpr long mod = 1000000007;
constexpr short shINF = 32767;
constexpr long loINF = 2147483647;
constexpr long long llINF = 9223372036854775807;
typedef long long LL;
typedef vector<LL> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<LL,LL> pr;
typedef vector<bool> VB;
typedef vector<pr> VP;
struct Order {
	bool operator() (pr const& a,pr const& b) const {
		return a.first > b.first || ((a.first == b.first) && (a.second > b.second));
	}
};
int n;
vector<int> w;
typedef bitset<301> bit;
struct Comp {
	bool operator() (const bit &a,const bit &b) {
		for (int i = 300-1; i >= 0; i--) {
        	if (a[i] ^ b[i]) return b[i];
    	}
    	return false;
	}
};
map<bit,int,Comp> memo;
int dfs(bit &state) {
	if (IN(state,memo)) return memo[state];
	int num = 0,fi;
	REP(i,n-1) {
		if ((!state[i])) {
			fi = i;
			break;
		}
	}
	FOR(i,fi+1,n) {
		if (!state[i]){
			if (abs(w[i] - w[fi]) <= 1) {
				state.set(i);state.set(fi);
				num = max(dfs(state)+2,num);
				state.reset(i);state.reset(fi);

			}
			fi = i;
		}
	}
	return memo[state] = num;
}

int main(void) {
	while (1) {
		cin >> n;
		if (n == 0) break;
		w.resize(n);
		REP(i,n) {
			cin >> w[i];
		}
		bit state(0);
		cout << dfs(state) << endl;
		memo.clear();
	}
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <cmath>

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,n) for(int i=(a);i<(n);i++)
using namespace std;

int main(){
  int n;
  while(cin>>n,n){
    vector<int> w(n);
    REP(i,n)cin>>w[i];
    bool dp[303][303];
    int ans[303];
    fill(dp[0],dp[n],false);
    fill(ans,ans+n,0);
    for(int i = 0;i <n;i++)dp[i][i+1] |= abs(w[i+1]-w[i])<=1;
    for(int width = 2;width <= n;width++){
      for(int l = 0;l + width <= n;l++){
				int r = l + width-1;
				for(int d = l;d < l+width-1;d++){//-1??¨???
					dp[l][r] |= dp[l][d]&&dp[d+1][r];
					dp[l][r] |= !(width&1) && dp[l+1][r-1] && abs(w[l]-w[r]) <=1;
				}
      }
    }
		/*		FOR(width,1,n+1){
			REP(l,n-width){
				cout << dp[l][l+width] << ' ';
				}
			cout << endl;
			}*/
    for(int i = 0;i<n;i++){
			//i-1?????§????????£????????????
			//i??\????????´??°??§??????????????????i???????????¶??§i-1??????????????????
			//ans[i]???????????´??°???????????§????????????divide??°??????????????????????????¢?????´??°???????????????
			if(i > 0)ans[i] = max(ans[i],ans[i-1]);
      for(int r = i+1;r<n;r++){
				//i-1?????§????????£??????????????????i???????§???????????¶????????????????????????¨?????´??°????????????
				if(dp[i][r]) ans[r] = max(ans[r],ans[i-1] + (r-i+1));
      }
    }
    cout << ans[n-1] << endl;
  }
  
  return 0;
}

C++: #define _GLIBCXX_DEBUG

#include <bits/stdc++.h>


using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = { 0 };
vector<int> w(n);

int rec(int l, int r) {
  if (r - 1 == l) {
    return 0;
  }
  
  if (r - l  - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return 2;
    }
  }
  if (memo[l][r]) {
    return memo[l][r];
  }
  
  int ret = 0;
  bool isOK = false;
  
  if (abs(w[l] - w[r - 1]) <= 1) {
    isOK = true;
  }


  for (int t = l + 1; t < r - 1; t++) {
    for (int k = t + 1; k < r; k++) {
      int temp = rec(t, k) + rec(k, r);
      ret = max(ret, ((isOK && temp == r - l - 2)? 2 : 0) + temp);   
    }
  }

  for (int t = l + 1; t < r - 1; t++) {
    int temp = rec(l, t) + rec(t, r);
    ret = max(ret, ((isOK && temp == r - l)? 2 : 0) + temp);
  }

  return memo[l][r] = ret;
}

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

    w.clear();
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    memset(memo, 0, sizeof(memo));
    cout << rec(0, n) << endl;

  }

  

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

vector<ll> w(301);
vector<vector<ll>> DP(301,vector<ll> (301,-1));

ll inf=1e18;

ll dp(ll l,ll h) {
  if(DP[l][h]>-1) {
    return DP[l][h];
  }
  if(h-l<=1) {
    return 0;
  }
  ll res=0;
  for(ll i=l+1;i<h;i++) {
    res=max(res,dp(l,i)+dp(i,h));
  }
  if(dp(l+1,h-1)==(h-l-2)&&abs(w[l]-w[h-1])<=1) {
    res=h-l;
  }
  return DP[l][h]=res;
}

int main() {
  while(1) {
    ll n;
    cin >> n;
    if(n==0) {
      break;
    }
    for(ll i=0;i<n;i++) {
      cin >> w[i];
    }
    for(ll i=0;i<=n;i++) {
      for(ll j=0;j<=n;j++) {
        DP[i][j]=-1;
      }
    }
    cout << dp(0,n) << endl;
  }
}