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 <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>
using namespace std;

vector<int> a;         //命令 a[1]-a[n] 1-n ->0-9,u
vector<vector<int> > ok;
//vector<vector<int> > doteki;
int n;
int sec(int p,int q);
void pr();

int main(){
  scanf ("%d",&n);
  while(n){
    int f=0;
    a.assign(n,0);
    ok.assign(n,a);
    //doteki.assign(n,a);
    for(int t=0;t<n;t++){
      scanf ("%d",&a[t]);
    }
    for(int s=0;(s+1)<n;++s){
      ok[s][s+1]=sec(s,s+1);
    }
    for(int t=3;t<n;t=t+2){
      for(int s=0;(s+t)<n;++s){
        f=0;
        for(int u=1;u<t-1;u=u+2){
          if(ok[s][s+u]&&ok[s+u+1][s+t]){
            f=1;
          }
        }
        if(f==1||(sec(s,s+t)&&ok[s+1][s+t-1])){
          ok[s][s+t]=t+1;
        }
      }
    }


    for(int x=1;x<n;++x){
      for(int y=0;y+x<n;++y){
        for(int z=y;z<y+x;++z){
          ok[y][y+x]=max(ok[y][y+x],ok[y][z]+ok[z+1][y+x]);
        }
        //pr();
      }
    }
    printf("%d\n",ok[0][n-1]);
    scanf ("%d",&n);
  }
}



//変化したところだけ見る



int sec(int p,int q){
  if(-1<=(a[p]-a[q])&&(a[p]-a[q])<=1){
    return 2;
    //ok[p][q]=1;
  }
  else{
    return 0;
    //ok[p][q]=0;
  }
}

void pr(){
  for(int x=0;x<n;++x){
    for(int y=0;y<n-1;++y){
      printf("%d ",ok[x][y]);
    }
    printf("%d\n",ok[x][n-1]);
  }
  printf("\n");
}

C++: #include<bits/stdc++.h>
using namespace std;

int dp[334][334];

int rec(vector<int> &w, int l, int r){
    if(dp[l][r] != -1) return dp[l][r];
    if(r-2 < l) return dp[l][r] = 0;
    if(l == r-2){
        if(abs(w[l] - w[r-1]) <= 1) return dp[l][r] = 2;
        else return dp[l][r] = 0;
    }
    int tmp = rec(w, l+1, r-1);
    if(abs(w[l] - w[r-1]) <= 1 && tmp == (r-l-2)){
        return dp[l][r] = tmp + 2;
    }
    for(int k=l+1; k<r; k++){
        tmp = max(tmp, rec(w, l, k)+rec(w, k, r));
    }
    return dp[l][r] = tmp;
}

int main(){
    int n;
    cin >> n;
    while(n > 0){
        vector<int> w(n);
        for(int i=0; i<n; i++){
            cin >> w[i];
        }
        memset(dp, -1, sizeof(dp));
        cout << rec(w, 0, n) << endl;
        cin >> n;
    }
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

bool ok(int a, int b) {
	return abs(a - b) <= 1;
}

vector<vector<int>>memo(1000, vector<int>(1000,-1));

int solve(const int l,const int r, vector<int>&v) {
	if (memo[l][r] != -1)return memo[l][r];
	if (l == r)return 0;
	else {
		if (l + 1 == r)return 0;
		else if (l + 2 == r) {
			if (ok(v[l],v[l+1]))return 2;
			else return 0;
		}
		else {
			int ans = 0;
			for (int k = l + 1; k < r - 1; ++k) {
				ans = max(ans, solve(l, k, v) + solve(k, r, v));
			}
			if (ok(v[l], v[r - 1]) && solve(l + 1, r - 1, v) == (r - l - 2)) {
				ans = r - l;
			}
			return memo[l][r]=ans;
		}
	}
}

int main() {
	while (1) {
		memo = vector<vector<int>>(1000, vector<int>(1000, -1));
		int N; cin >> N;
		vector<int>v(N);
		for (int i = 0; i < N; ++i)cin >> v[i];
		if (!N)break;
		int ans = solve(0, N, v);
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(b);i>=(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

#define N 305
ll n;
ll w[N];
ll dp[N][N];
ll f(ll l,ll r){
    if(r-l<2)return 0;
    if(dp[l][r]!=-1)return dp[l][r];
    ll ret=0;
    if(abs(w[l]-w[r-1])<=1&&f(l+1,r-1)==r-l-2)ret=r-l;
    fore(i,l+1,r-1){
        ll tmp=f(l,i)+f(i,r);
        chmax(ret,tmp);
    }
    return dp[l][r]=ret;
}
void solve(){
    while(1){
        cin>>n;
        if(n==0)return;
        rep(i,n)cin>>w[i];

        memset(dp,-1,sizeof(dp));
        cout<<f(0,n)<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve();
}

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

int main() {
  unsigned int N = 0;
  vector<int> num;
  int w = 0;
  vector<int> answer;

  cin >> N;
  while(N) {
    for(unsigned int i = 0; i < N; i++) {
      cin >> w;
      num.push_back(w);
    }

    while(true) {
      unsigned int i;
      for(i = num.size()-1; i > 0; i--)
	if(num.at(i)-num.at(i-1) >= -1 && num.at(i)-num.at(i-1) <= 1)
	  break;

      if(i == 0)
	break;
      num.erase(num.begin()+i);
      num.erase(num.begin()+i-1);
      
      if(num.empty())
	break;
    }

    answer.push_back(N-num.size());

    num.clear();

    cin >> N;
  }

  for(unsigned int j = 0; j < answer.size(); j++)
    cout << answer.at(j) << endl;

  return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0; i<n; ++i)
#define OK(x,y) abs(w[x]-w[y])<=1
int n, w[311];
int dp[311][311];

int main(){
  while(cin>>n&&n){
    REP(i,n) cin>>w[i];
    memset(dp, 0, sizeof dp);

    for(int j=1; j<n; j++){
      for(int i=0; i+j<n; i++){
        if(OK(i,i+j) && dp[i+1][i+j-1]==j-1){
          dp[i][i+j]=j+1;
        }

        for(int k=i; k<=i+j; k++){
          dp[i][i+j]=max(dp[i][i+j], dp[i][k]+dp[k+1][i+j]);
        }
      }
    }
    cout << dp[0][n-1] << endl;
  }
  return 0;
}