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

//#define MAX 300
short n;
short* w;
vector<pair<short*,short> > wmap;

short* pop(short* w,short a) { //w???a????????¨a+1?????????????????????
	short *t;
	t = new short[n];
	for (short i = 0; i < n; i++) {
		if(i < a) t[i] = w[i];
		else t[i] = w[i + 2];
	}
	t[n - 2] = -1;
	t[n - 1] = -1;
	return t;
}

short daruma(short* w) {
	for (short i = 0; i < wmap.size(); i++) {
		if (memcmp(w, wmap[i].first, sizeof(w)) == 0) {
			return wmap[i].second;
		}
	}

	short max = 0;
	for (short i = 0; i < n - 1; i++) {
		if (w[i] != -1 && w[i + 1] != -1 && abs(w[i]-w[i+1])<=1) {
			short tmp = daruma(pop(w, i))+2;
			if (tmp > max) max = tmp;
		}
	}

	wmap.push_back(make_pair(w,max));
	return max;
}

void clear() {
	wmap.clear();
}

int main() {
	cin >> n;
	while (n) {
		clear();
		w = new short[n]();
		for (short i = 0; i < n; i++) {
			cin >> w[i];
		}
		cout << daruma(w) << endl;

		delete(w);
		cin >> n;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>

using namespace std;
typedef long long ll;

int main(){
	while(1){
		int n;
		int w[301];
		int dp[301][301];
		scanf("%d",&n);
		if(n==0) break;
		
		for(int i=0;i<n;i++) scanf("%d ",&w[i]);
		for(int i=0;i<301;i++) fill_n(dp[i],301,0);
		
		for(ll W=2;W<n+1;W++){
			for(int i=0;i<n;i++){
				int j=i+W-1;
				if(j>=n) continue;
				if(dp[i+1][j-1]==W-2 && abs(w[i]-w[j])<=1)
				dp[i][j]=W;
				for(ll k=i;k<j;k++){
					dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
				}
			}
		}
		printf("%d\n",dp[0][n-1]);
	}
}

C++: #include<iostream>
#include<vector>
#include<tuple>
#include<algorithm>
#include<cmath>
using namespace std;
vector<tuple<int, int, int>>vec; int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; vec.clear(); 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; i++) { for (int j = i + 1; j < n; j++) { vec.push_back(make_tuple(j - i, i, j)); } }
		sort(vec.begin(), vec.end());
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 0; i < vec.size(); i++) {
			int a1 = get<1>(vec[i]), a2 = get<2>(vec[i]);
			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 > 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 = 0; i < n; i++) { for (int j = i; j < n; j++) { for (int k = j; k < n; k++) { dp[i][k] = max(dp[i][k], dp[i][j - 1] + dp[j][k]); } } }
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

C++: /*
    Problem 48
    https://onlinejudge.u-aizu.ac.jp/problems/1611
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 306;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
int n;
vector<int> w(MAX);
vector<vector<int>> dp(MAX, vector<int>(MAX));
/* function */
void inputW() {for (int i = 0; i < n; i++) cin >> w[i];}
void init() { for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) dp[i][j] = 0; }
void calcDP() {
    // dp[s][t] = s ~ t番目を使ってられる最大値
    // s |s+1| ... |t-1| tの時の遷移
    // 1) |s+1| ~ |t-1| まで全部落とせる場合
    //    if (dp[s+1][t-1] == t-s-1 && abs(w[s] - w[t]) <= 1)
    //      dp[s][t] = dp[s+1][t-1] + 2
    // 2) 分割候補を探索して最大値を探す
    //    for k = s+1, ..., t-1
    //      dp[s][t] = max(dp[s][t], dp[s][k] + dp[k+1][t])
    for (int i = 1; i < n; i++) {  // s ~ t(=s+i)まで
        for (int s = 0; s < n - i; s++) {  // start位置
            int t = s + i;  // end位置
            // 1)
            if (dp[s+1][t-1] == t-s-1)
                dp[s][t] = dp[s+1][t-1] + (abs(w[s] - w[t]) <= 1 ? 2 : 0);
            // 2)
            for (int k = s+1; k <= t-1; k++)
                dp[s][t] = max(dp[s][t], dp[s][k] + dp[k+1][t]);
        }
    }
}
void printAns() { cout << dp[0][n-1] << '\n'; }
/* main */
int main(){
    while (cin >> n) {
        if (n == 0) return 0;
        inputW();
        init();
        calcDP();
        printAns();
    }
}

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

using namespace std;

vector<vector<int>> mem;
vector<int> bs;

int rec(int l, int r) {
  if(mem[l][r] >= 0) return mem[l][r];
  if(l >= r) {
    mem[l][r] = 0;
  } else {
    int d = r - l + 1;
    if(rec(l+1, r-1) == d - 2 && abs(bs[l] - bs[r]) <= 1) {
      mem[l][r] = d;
    } else {
      int ans = 0;
      for(int k = l; k < r; k++) {
        ans = max(ans, rec(l, k) + rec(k+1, r));
      }
      mem[l][r] = ans;
    }
  }
  return mem[l][r];
}

int main(int argc, char *argv[])
{
  for(;;) {
    int m;
    cin >> m;
    if(m == 0) break;
    bs.clear();
    for(int i = 0; i < m; i++) {
      int b;
      cin >> b;
      bs.push_back(b);
    }
    mem = vector<vector<int>>(m, vector<int>(m, -1));
    int ans = rec(0, m-1);
    cout << ans << endl;
  }
  return 0;
}

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

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) break;
		vector<int> w(n);
		rep(i, n) cin >> w[i];
		vector<vector<int>> dp(n + 1, vector<int>(n + 1));
		for (int d = 2; d <= n; d++) {
			for (int l = 0; l <= n - d; l++) {
				int r = l + d;
				if (dp[l + 1][r - 1] == r - l - 2 && abs(w[l] - w[r - 1]) < 2) {
					dp[l][r] = r - l;
				}
				for (int m = l + 1; m < r; m++) {
					dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
				}
			}
		}
		cout << dp[0][n] << endl;
	}
	return 0;
}