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 <vector>
#include <algorithm>

using namespace std;


int n;
vector<int> w;

enum MEMO {
  NOT_CHECKED,
  TRUE,
  FALSE
};


MEMO memo[301][301];

MEMO rec(int l, int r) {
  if (r == l) {
    return TRUE;
  }
  if (r - l - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return memo[l][r] = TRUE;
    } else {
      return memo[l][r] = FALSE;
    }
  }

  if (r - 1 == l) {
    return memo[l][r] = FALSE;
  }

  if (memo[l][r] != NOT_CHECKED) {
    return memo[l][r];
  }

  if (abs(w[l] - w[r - 1]) <= 1) {
    for (int i = l + 1; i < r; i++) {
      int temp1 = rec(l + 1, i);
      int temp2 = rec(i, r - 1);
      if (temp1 == TRUE && temp2 == TRUE) {
        return memo[l][r] = TRUE;
      }
    }
  }

  for (int i = l + 1; i < r; i++) {
    int temp1 = rec(l, i);
    int temp2 = rec(i, r);
    if (temp1 == TRUE && temp2 == TRUE) {
      return memo[l][r] = TRUE;
    }
  }

  return memo[l][r] = FALSE;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

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

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

    rec(0, n);

    int dp[301] = { 0 };
    for (int r = 1; r <= n; r++) {
      dp[r] = dp[r - 1];
      for (int l = 0; l < r; l++) {
        if (memo[l][r] == TRUE) {
          dp[r] = max(dp[r], dp[l] + (r - l));
        }
      }
    }

    int ans = dp[n];
    cout << ans << endl;
  }

  return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const int INF = 1LL << 29;

int main() {
    int N, w[300];
    while (true) {
        cin >> N;
        if (N == 0) break;
        for (int j = 0; j < N; ++j) cin >> w[j];

        int dp[305][305];
        for (int j = 0; j < N; ++j) {
            fill(dp[j], dp[j]+N+1, 0);
        }

        for (int j = 0; j+1 < N; ++j) {
            if (abs(w[j] - w[j+1]) <= 1) {
                dp[j][j+2] = 2;
            }
        }

        for (int d = 3; d <= N; ++d) {
            for (int j = 0; j + d <= N; ++j) {
                for (int k = 1; j+k < j+d; ++k) {
                    dp[j][j+d] = max(dp[j][j+d], dp[j][j+k] + dp[j+k][j+d]);
                }

                if (dp[j+1][j+d-1] == d-2 && abs(w[j] - w[j+d-1]) <= 1) {
                    dp[j][j+d] = d;
                }
            }
        }

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

C++: #include <iostream>

using namespace std;

#define REP(i, n) for(int i=0; i<n; i++)

int n;
int w[400];
int dp[400][400];

// the maximum number of block can be removed
int cnt(int b, int t) {
    if (dp[b][t] >= 0) return dp[b][t];

    if (abs(w[t]-w[b])<=1 && cnt(b+1, t-1) == t-(b-1)-2) {
        return dp[b][t] = t-(b-1);
    }

    int output = 0;
    for(int i=b; i<=t-1; i++) {
        output = max(output, cnt(b, i)+cnt(i+1, t));
    }
    return dp[b][t] = output;
}

int main() {
    while(true) {
        cin >> n;
        if (n==0) break;

        REP(i, n) {
            cin >> w[i];
        }

        REP(i, n) {
            REP(j, n) {
                if (i==j) {
                    dp[i][j] = 0;
                } else if (j-i == 1){
                    if (abs(w[i] - w[j]) <= 1) {
                        dp[i][j] = 2;
                    } else {
                        dp[i][j] = 0;
                    }
                } else {
                    dp[i][j] = -1;
                }
            }
        }

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

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

int main(){

	int N,data[301];
	bool** table = new bool*[301]; //table[left][right] = left???????§?????????????2*right??????????????¨?¶??????§??????????????????
	int** maximum = new int*[301]; //?????????????????????????????§?¶????????????°
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		maximum[i] = new int[301];
	}

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		if(N <= 1){
			printf("0\n");
			continue;
		}

		//?¶??????????????????????????????????
		for(int i = 1; i <= N; i++){
			for(int k = 0; k <= N/2; k++)table[i][k] = false;
			for(int k = 1; k <= N; k++)maximum[i][k] = 0;
		}

		for(int i = 1; i <= N; i++)scanf("%d",&data[i]);

		//????????????2???????¨?
		for(int i = 1; i <= N-1; i++){
			if(abs(data[i+1]-data[i]) <= 1){
				table[i][1] = true;
				maximum[i][i+1] = 2;
			}
		}

		//?¶??????????????????????????¨????
		for(int length = 4; length <= N; length += 2){ //??????????????????
			for(int left = 1; left+length -1 <= N; left++){ //??????????????????
				if((table[left][1] == true && table[left+2][(length-2)/2] == true) || //????????¨?????£???????¶????????????????????????????2????????£??????length-2??????????¶?????????´???
					(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)){ //??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???
					table[left][length/2] = true;
					maximum[left][left+length-1] = length; //????¨?????????§??????OK????????§?????¶????????§???????¨????
				}else{
					//maximum[left][left+length-1]?????´??°????????°????????????
					for(int i = 2; i <= length-2; i+= 2){
						maximum[left][left+length-1] = max(maximum[left][left+length-1],maximum[left][left+i-1]+maximum[left+i][left+length-1]);
					}
				}
			}
		}

		if(N%2 == 0)printf("%d\n",maximum[1][N]);
		else{
			printf("%d\n",max(maximum[1][N-1],maximum[2][N]));
		}
	}

	return 0;
}