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>
using namespace std;

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

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

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

  cin >> N;
  while(N != 0) {
    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;
    }

    cout << N-num.size() << endl;

    num.clear();

    cin >> N;
  }


  return 0;
}

C++: // g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
//typedef ll Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int n;
vi in;
int dp[310][310];
int f(int l,int r){
	if(dp[l][r]+inf)return dp[l][r];
	if(r-l<2)return dp[l][r]=0;
	if(r-l==2&&abs(in[l]-in[r-1])<2)return dp[l][r]=2;
	int out=-inf;
	loop(i,l+1,r-1)out=max(out,f(l,i)+f(i,r));
	
//	loop(i,l,r-1)if(abs(in[i]-in[i+1])<2){
//		out=max(out,f(l,i)+2+f(i+2,r));
//	}
	int t=f(l+1,r-1);
	if(t==r-l-2){
		if(abs(in[l]-in[r-1])<2)out=max(out,r-l);
	}
	out=max(out,f(l+1,r));
	out=max(out,f(l,r-1));
	return dp[l][r]=out;
}
int main(){
	while(cin>>n,n){
		in=vi(n);
		rep(i,n)cin>>in[i];
		rep(i,310)rep(j,310)dp[i][j]=-inf;
		cout<<f(0,n)<<endl;;
	}
}

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

int main(void){
  int n;
  cin >> n;
  
  while(n != 0){   
    vector<int> w(n);
    for(int i=0; i<n; i++)
      cin >> w[i];

    vector< vector<int> > dp(n, vector<int>(n));

    if(n == 1){
      cout << 0 << endl;
      cin >> n;
      continue;
    }

    for(int i=0; i<n-1; i++)
      if(abs(w[i] - w[i+1]) <= 1)
	dp[i][i+1] = 2;

    for(int k=3; k<n; k+=2){
      for(int i=0; i<n; i++){
	if(i+k >= n)
	  break;

	int nmax = 0;	
	if(dp[i+1][i+k-1] == k-1){
	  if(abs(w[i] - w[i+k]) <= 1){
	    dp[i][i+k] = dp[i+1][i+k-1] + 2;
	    continue;
	  }
	  nmax = dp[i+1][i+k-1];
	}

	for(int j=i+1; j<i+k; j+=2){
	  int temp = dp[i][j] + dp[j+1][i+k];
	  if(temp > nmax){
	    nmax = temp;
	  }
	}
	dp[i][i+k] = nmax;
      }
    }    

    if(n%2 != 0){
      int nmax = max(dp[1][n-1], dp[0][n-2]);
      for(int i=2; i<n-1; i+=2){
	int temp = dp[0][i-1] + dp[i+1][n-1];
	if(temp > nmax)
	  nmax = temp;
      }
      dp[0][n-1] = nmax;
    }
    
    cout << dp[0][n-1] << endl;

    cin >> n;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; }
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 2; i <= n; i++) {
			for (int j = 0; j < n; j++) {
				int a1 = j, a2 = i + j - 1; if (a2 >= n)continue;
				if (dp[a1][a2] == 0)continue;
				if (a2 < n - 2 && abs(a[a2 + 1] - a[a2 + 2]) <= 1)dp[a1][a2 + 2] = 1;
				if (a1 > 1 && abs(a[a1 - 1] - a[a1 - 2]) <= 1)dp[a1 - 2][a2] = 1;
				if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)dp[a1 - 1][a2 + 1] = 1;
			}
		}
		for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[i][j] == 1)dp[i][j] = j - i + 1; } }
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				for (int k = n; k >= 0; k--) {
					int a1 = k, a2 = k + i, a3 = k + i + j; if (a3 > n)continue;
					dp[a1][a3 - 1] = max(dp[a1][a3 - 1], dp[a1][a2 - 1] + dp[a2][a3 - 1]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}