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 <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],ans;
	bool** table = new bool*[301];
	int** dp = new int*[301];
	for(int i = 0; i <= 300; i++){
		table[i] = new bool[151];
		dp[i] = new int[301];
	}

	bool FLG;

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

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

		for(int i = 0; i <= 300; i++){
			for(int k = 0; k <= 150; k++)table[i][k] = false;
			for(int k = 0; k <= 300; k++){
				dp[i][k] = 0;
			}
		}

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

		for(int i = 1; i <= N-1; i++){
			if(abs(data[i+1]-data[i]) <= 1){
				table[i][1] = true;
				dp[i][i+1] = 2;
			}else{
				dp[i][i+2] = 0;
			}
		}

		for(int length = 4; length <= N; length += 2){
			for(int left = 1; left+length -1 <= N; left++){
				FLG = false;

				if(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)FLG = true;

				if(!FLG){
					for(int i = 2; i <= length-2; i += 2){
						if(table[left][i/2] == true && table[left+i][(length-i)/2] == true){
							FLG = true;
							break;
						}
					}
				}
				if(FLG){
					table[left][length/2] = true;
					dp[left][left+length-1] = length;
				}
			}
		}

		ans = 0;

		for(int length = 4; length <= N; length += 2){ //??????????????????
			for(int left = N-(length/2);left >= N-length+2; left--){ //????????¨???????????????
				dp[left][N] = max(dp[left+1][left+(length-2)-1],max(dp[left][left+(length-2)+1],dp[left+1][N])); //?????????????????????????????§????????????
			}
			for(int left = N-length+1; left >= 1; left--){ //?§????????????????
				for(int mid = left+1; mid <= N-2; mid++){
					dp[left][left+length-1] = max(dp[left][left+length-1],dp[left][mid]+dp[mid+1][left+length-1]);
					ans = max(ans,dp[left][left+length-1]);
				}
			}
		}
		/*for(int i = 1; i <= N-1; i++){
			for(int k = i+1; k <= N; k++){
				printf("dp[%d][%d]:%d\n",i,k,dp[i][k]);
			}
		}*/
		printf("%d\n",ans);
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)

using namespace std;

int main() {

  int n, a[305];
  while(1){
    cin>>n;
    if(n == 0) break;
    REP(i,0,n) cin>>a[i];

    int dp[305][305];
    REP(i,0,n)REP(j,0,n) dp[i][j] = 0;

    for(int k=1; k<n; k++){
      for(int i = 0; i+k<n; i++){
        if(abs(a[i] - a[i+k]) <= 1 && dp[i+1][i+k-1] == (i+k) - i - 1){
          dp[i][i+k] = dp[i+1][i+k-1] + 2;
        }else{
          int tmp = 0;
          for(int p=i+1; p<i+k; p++){
            if(p+1 < i+k) tmp = max(tmp, dp[i][p] + dp[p+1][i+k]);
            else tmp = max(tmp, max(dp[i][p], dp[i+k-1][i+k]));
          }
          dp[i][i+k] = tmp;
        }
      }
    }
    cout<<dp[0][n-1]<<endl;
  }


  return 0;
}

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

using namespace std;

#define INFI 1e9;

int main() {
	int n;
	while (1) {
		cin >> n;
		if (n == 0)break;
		int* w = new int[n];
		for (int i = 0;i < n;i++) {
			cin >> w[i];
		}
		int** dp = new int*[n];
		for (int i = 0;i < n;i++) {
			dp[i] = new int[n - i];
			dp[i][0] = 1;
			if (i != n - 1) {
				if (abs(w[i] - w[i + 1]) <= 1) {
					dp[i][1] = 0;
				}
				else {
					dp[i][1] = 2;
				}
			}
				
		}
		for (int i = 2;i < n;i++) {
			for (int j = 0;j < n - i;j++) {
				if (dp[j + 1][i - 2] == 0 && abs(w[j] - w[j + i]) <= 1) {
					dp[j][i] = 0;
				}
				else {
					dp[j][i] = INFI;
					for (int k = 0;k < i;k++) {
						dp[j][i] = min(dp[j][i], dp[j][k] + dp[j + k + 1][i - k - 1]);
					}
				}
			}
		}
		cout <<n - dp[0][n - 1] << endl;
	}
	
}

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

using namespace std;

vector<vector<int>> dp;  
vector<int> w;

int rec(int l,int r){
  if(dp[l][r]!=-1)return dp[l][r];
  if(abs(r-l)<=1)return 0;
  int res=0;
  if(abs(w[l]-w[r-1])<=1 && rec(l+1,r-1)==r-l-2)res=r-l;

  for(int mid=l+1;mid<r;mid++){
    res=max(res,rec(l,mid)+rec(mid,r));
  }

  return dp[l][r]=res;

}

int main(){

  int n;
  cin >> n;

  while(n!=0){
    w.resize(n);
    for(int i=0;i<n;i++)cin >> w[i];

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

    cout << rec(0,n) << endl;

    cin >> n;
  }

}

C++: #include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<functional>
#include<set>
using namespace std;

#define INF 1<<21
#define DIV 1000000007
#define MAX 250000


int main(){
  int n;
  while (cin >> n, n) {
	int w[305];
	int dp[305][305];
	memset(dp, 0, sizeof(dp));
	for (int i = 0; i < n; i++) {
	  cin >> w[i];
	}

	for (int i = 1; i < n; i++) {
	  for (int j = 0; j + i < n; j++) {


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


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


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