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 <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <tuple>
#include <iomanip>
#include <climits>
#include <fstream>
using namespace std;

const int INF = 1<<30;
#define rep(i, n) for(int i = 0; i < (n); i++)


int N;
vector<int> w;
int can[310][310];
int dp[310];
bool saiki(int l, int n){
  if(can[l][n] != -1) return can[l][n];
  if(n % 2) return can[l][n] = 0;
  if(n == 0) return can[l][n] = 1;

  bool res = 0;
  for(int i = 1; i <= n-1; i++) if(saiki(l, i) && saiki(l+i, n-i)) res = 1;
  if(saiki(l+1, n-2) && abs(w[l] - w[l+n-1]) <= 1) res = 1;
  return can[l][n] = res;
}


void solve(){
  rep(i, 310)rep(j, 310) can[i][j] = -1;
  rep(i, N)rep(j, N+1){
    if(i+j<=N) saiki(i, j);
  }
  rep(i, 310) dp[i] = -INF;
  dp[0] = 0;
  rep(i, N){
    dp[i+1] = max(dp[i+1], dp[i]);
    for(int j = 1; j <= N; j++){
      if(i+j<=N && can[i][j]) dp[i+j] = max(dp[i+j], dp[i] + j);
    }
  }
  cout << dp[N] << endl;
}


int main(){
  while(true){
    cin >> N;
    if(N == 0) return 0;
    w.resize(N);
    rep(i, N) cin >> w[i];
    solve();
  }

}

C++: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;

int n;
vector<int> w;

int dp[333][333];

int rec(int l, int r)
{
  if(l == r) return 0;
  if(l + 1 == r) return abs(w[r] - w[l]) <= 1 ? 2 : 0;

  int& ret = dp[l][r];
  if(~ret) return ret;
  ret = 0;

  if(abs(w[r] - w[l]) <= 1) {
    int temp = rec(l+1, r-1);
    if(temp == r-l-1) ret += temp + 2;
  }

  for(int i = l; i < r; i++) {
    ret = max(ret, rec(l, i) + rec(i+1, r));
  }
  
  return ret;
}

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

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

using namespace std;

int main(){
    vector<int> ans;

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

        if(n==0) break;

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

        int dp[n][n];
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                dp[i][j] = 0;
            }
        }

        for(int l=2; l<=n;l++){
            for(int i=0; i<n; i++){
                int j = i + l - 1;
                if(j>=n) continue;
                if(dp[i+1][j-1] == j-i-1 && abs(w[i]-w[j])<=1){
                    dp[i][j] = j - i + 1;
                }else{
                    for(int k=0; k<n-1; k++){
                        dp[i][j] = max(dp[i][j], dp[i][k]+dp[k+1][j]);
                    }
                }
            }

        }

        ans.push_back(dp[0][n-1]);
    }

    for(auto it=ans.begin(); it != ans.end(); it++){
        cout << *it << endl;
    }

    return 0;
}

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

int main(void) {
  int N;
  while (cin >> N, N) {
    vector<int> W(N);
    for (int i = 0; i < N; i++) cin >> W[i];

    int dp[300][300] = {};
    for (int l = 0; l + 1 < N; l++) {
      if (abs(W[l] - W[l+1]) > 1) continue;
      dp[l][l+1] = 2;
    }

    for (int d = 3; d <= N; d++) {
      for (int l = 0; l + d <= N; l++) {
        int r = l + d - 1;
        dp[l][r] = dp[l+1][r-1];
        if (abs(W[l] - W[r]) <= 1 && dp[l+1][r-1] == r - l - 1)
          dp[l][r] += 2;

        for (int m = l + 1; m <= r - 1; m++) {
          dp[l][r] = max(dp[l][r], dp[l][m] + dp[m+1][r]);
        }
      }
    }

    cout << dp[0][N-1] << endl;
  }
  return 0;
}

C++: #include <iostream>

#define REP(i, n) for (int i = 0; i < n; i++)
#define debug(x) cerr << #x << " = " << (x) << endl
#define SIZE 302

using namespace std;

int dp[SIZE][SIZE], w[SIZE];

int f(int j, int k) {
	// printf("f(%d, %d)\n", j, k);
	int maxf = -1;
	if (dp[j][k] > 0) return dp[j][k];
	if (j == k) return 0;
	if (k - j == 1) {
		dp[j][k] = ((abs(w[j] - w[k])) <= 1 ? 2 : 0);
		/*debug(j);
		debug(k);
		debug(dp[j][k]);*/
		return dp[j][k];
	}
	for (int i = j + 1; i + 1 < k; i++) {
		maxf = max(maxf, f(j, i) + f(i + 1, k));
	}
	dp[j][k] = max(f(j + 1, k - 1) + (abs(w[j] - w[k]) <= 1 ? 2 : 0), maxf);
	/*debug(j);
	debug(k);
	debug(dp[j][k]);*/
	return dp[j][k];
}

int main() {
	while (1) {
		int n;

		cin >> n;
		if (n==0) break;
		REP(i, n) cin >> w[i];
		REP(j, n) { REP(k, n) dp[j][k] = -1; }

		cout << f(0, n - 1) << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b){ return (a<b ? (a=b)||1 : 0); }

int main() {
	
	while(1){
		int n; cin >> n; if(!n) break;
		vector<int>w(n);
		for(int i=0; i<n; i++) cin>>w.at(i);
		vector<vector<int>>dp(n+1,vector<int>(n+1));
		for(int r=1; r<n; r++){
			for(int i=0, j; (j=i+r)<n; i++){
				for(int k=i; k<j; k++){
					if(r==1&&abs(w.at(i)-w.at(j))<=1) dp.at(i).at(j)=2;
					if(r>1){
						chmax(dp.at(i).at(j), dp.at(i).at(k)+dp.at(k+1).at(j));
						if(dp.at(i+1).at(k)+dp.at(k+1).at(j-1)==j-i-1&&abs(w.at(i)-w.at(j))<=1) dp.at(i).at(j)=j-i+1;
					}
					
				}
			}
		}
		cout << dp.at(0).at(n-1) << endl;
	}
	
}