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;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
constexpr ll INF = (1LL << 55);

int main(){
  while (1) {
  int n;
  cin >> n;
  if (!n) break;
  vector<int> w(n);
  rep(i,n) cin >> w[i];

  vector<vector<int>> dp(n+1,vector<int>(n+1,0));

  for (int len = 2; len <= n; len++) {
    for (int l = 0; l + len <= n; l++) {
      int r = l + len;
      if (abs(w[r-1] - w[l]) <= 1 && dp[l+1][r-1] == len - 2) dp[l][r] = len;
      for (int k = l; k <= r; k++) {
        dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r]);
      }
    }
  }
  cout << dp[0][n] << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#define REP(i,n,N) for(int i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#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 int inf=1e9;

int dp[310][310];
int n;
int w[310];
int main() {
    while(cin>>n, n){
        REP(i,0,n) REP(j,0,n) dp[i][j] = 0;
        REP(i,0,n) cin>>w[i];
        for(int k=1;k<n;k++){
            REP(L,0,n-k){
                int R = L + k;
                if(dp[L+1][R-1] == k-1) dp[L][R] = max(dp[L][R], dp[L+1][R-1] + (abs(w[L]-w[R]) <= 1)*2);
                REP(i,L,R) dp[L][R] = max(dp[L][R], dp[L][i] + dp[i+1][R]);
            }
        }
        p(dp[0][n-1]);
    }


    return 0;
}

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

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

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

vector<int>as;
bool ok(int a,int b){
  return abs(as[a]-as[b])<=1;
}

int main() {
  while(1){
    
    int N;cin>>N;
    if(!N)break;
    as.resize(N);
    vector<vector<int>>dp(N+1,vector<int>(N+1,-100000));
    for(int i=0;i<N;++i){
      int a;cin>>a;as[i]=a;
    }
    for(int i=0;i<=N;++i){
      dp[i][i]=0;
    }
    for(int t=2;t<=N;++t){
      for(int l=0;l<=N-t;++l){
	const int r=l+t;
	dp[l][r]=max(dp[l+1][r],dp[l][r-1]);
	if(ok(l,l+1))dp[l][r]=max(dp[l+2][r]+2,dp[l][r]);
	if(ok(r-2,r-1))dp[l][r]=max(dp[l][r-2]+2,dp[l][r]);
	if(ok(l,r-1)){
	  if(dp[l+1][r-1]==(r-l-2)){
	    dp[l][r]=max(dp[l+1][r-1]+2,dp[l][r]);
	  }
	}
      } 
    }

    int ans=dp[0][N];
    ans=max(0,ans)
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using namespace std;

#define INF ((1<<30)-1)
#define LLINF (1LL<<60)
#define EPS (1e-10)

bool flag[501][501];
int dp[501][501];

int main() {

    int n;
    while (cin >> n and n) {
        vector<int> v(n);
        rep(i, n) cin >> v[i];

        fill(flag[0], flag[350], false);
        fill(dp[0], dp[350], 0);

        // [i, j) という区間がどうなのかを見たい
        for (int len = 1; len <= n; ++len) {
            for (int i = 0; i + len <= n; ++i) {
                int j = i + len;

                if (dp[i+1][j-1] == (j-1) - (i+1) and abs(v[i] - v[j-1]) <= 1) {
                    dp[i][j] = max(dp[i][j], dp[i+1][j-1] + 2);
                }

                for (int k = i; k <= j; ++k) {
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
                }
            }
        }

        cout << dp[0][n] << endl;
    }
}

C++: #include <iostream>
#include <cmath>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
using namespace std;

int N, W[300];
int dp[301][3001];

bool ok(int i, int j) {
  return abs(W[i] - W[j]) <= 1;
}

int dfs(int l, int r) {
  if(r - l <= 1) return 0;
  if(r - l == 2) return ok(l, l + 1) ? 2 : 0;
  if(dp[l][r] >= 0) return dp[l][r];

  int ret = 0;
  REP(i, l + 1, r - 1) ret = max(ret, dfs(l, i) + dfs(i, r));
  if(ok(l, r - 1) && dfs(l + 1, r - 1) == r - l - 2) ret = max(ret, r - l);

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

int main(void) {
  while(cin >> N, N) {
    REP(i, 0, N) cin >> W[i];
    REP(i, 0, 301) REP(j, 0, 301) dp[i][j] = -1;
    cout << dfs(0, N) << endl;
  }
}