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>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int dp[400][400],dp2[400][400];
int w[400];

int rec(int l,int r){
	if(r-l==0)return true;
	if(dp[l][r]!=-1)return dp[l][r];
	for(int k=l+1;k<r;k++){
		if(abs(w[l]-w[k])<=1){
			if(rec(l+1,k)&&rec(k+1,r))return dp[l][r]=true;
		}
	}
	return dp[l][r]=false;
}
int rec2(int l,int r){
	if(r-l==0)return 0;
	if(dp2[l][r]!=-1)return dp2[l][r];
	if(rec(l,r))return r-l;
	dp2[l][r]=0;
	for(int k=l+1;k<r;k++){
		dp2[l][r]=max(dp2[l][r],rec2(l,k)+rec2(k,r));
	}
	return dp2[l][r];
}
int main(){
	int n;
	while(scanf("%d",&n),n){
		rep(i,n)scanf("%d",&w[i]);
		memset(dp,-1,sizeof(dp));
		memset(dp2,-1,sizeof(dp2));
		cout<<rec2(0,n)<<endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
#include <cmath>

using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);++(i))

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

		for(int W = 2;W <= n;W++)
		{
			//left
			for(int l = 0;l < n;l++)
			{
				int r = l + W - 1;
				if(r >= n) continue;

				if(dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r]) <= 1)
					dp[l][r] = W;

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

			}

		}

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

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

using namespace std;

struct Info{
	Info(){
		loc = div = 0;
	}
	int loc,div;
};

int N,maximum;

void recursive(int table[],int num,int SIZE){

	maximum = max(maximum,2*num);

	int max_chain = 0,tmp_chain,next_index = 0;
	Info nextTable[SIZE];

	int count = 0;
	for(int i = 1; i <= SIZE-1; i++){
		if(abs(table[i] - table[i-1]) <= 1){
			count++;
			if(i == SIZE-1){
				if(max_chain == 0){
					nextTable[next_index].loc = SIZE-1;
					nextTable[next_index++].div = 0;
				}
			}else{
				tmp_chain = 0;
				for(int k = 1;(i-1)-k>=0 && i+k <= SIZE-1;k++){
					if(abs(table[(i-1)-k] - table[i+k]) <= 1){
						tmp_chain++;
					}
					else{
						break;
					}
				}
				if(tmp_chain > max_chain){
					max_chain = tmp_chain;
					next_index = 0;
					nextTable[next_index].loc = i;
					nextTable[next_index++].div = tmp_chain;
				}else if(tmp_chain == max_chain){
					nextTable[next_index].loc = i;
					nextTable[next_index++].div = tmp_chain;
				}
			}
		}
	}

	if(2*(num+count) <= maximum)return;

	for(int i = 0; i < next_index;i++){ //?????????????????¨???????????¨???????????????????????????
		int work[SIZE],new_table[SIZE],next_size = 0;
		for(int k = 0; k < SIZE; k++)work[k] = table[k];
		work[nextTable[i].loc-1] = -1;
		work[nextTable[i].loc] = -1;

		for(int k = 1; k <= nextTable[i].div; k++){
			work[nextTable[i].loc-1-k] = -1;
			work[nextTable[i].loc + k] = -1;
		}

		for(int k = 0; k < SIZE; k++){
			if(work[k] != -1){
				new_table[next_size++] = work[k];
			}
		}
		recursive(new_table,num+nextTable[i].div+1,next_size);
	}
}

void func(){

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

	if(N == 1){
		printf("0\n");
		return;
	}else if(N == 2){
		if(abs(first[0] - first[1] <= 1)){
			printf("1\n");
		}else{
			printf("0\n");
		}
		return;
	}

	recursive(first,0,N);

	printf("%d\n",maximum);
}


int main(){

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

		func();
	}

    return 0;
}

C++: #include <bits/stdc++.h>
#define N 301
using namespace std;
int w[N],mem[N][N],used[N][N];

int dfs(int l,int r){
  if(l==r) return 0;
  if(r-l==1) return 2*(abs(w[l]-w[r])<=1);
  if(used[l][r]++) return mem[l][r];
  
  int res=0;
  
  if((r-l)%1==0&&abs(w[l]-w[r])<=1&&
     dfs(l+1,r-1)==r-l-1) res=2+dfs(l+1,r-1);
  
  for(int i=l;i<r;i++)
    res=max(res,dfs(l,i)+dfs(i+1,r));
  
  return mem[l][r]=res;
}

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

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <math.h>
#include <set>
  
#define REP(i,n)for (int i=0;i<(n);i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ll long long
using namespace std;
typedef pair<int,int> P;
int N, W[300], dp[300][300];
  
int rec(int l,int r){
	if(l>=r)return 0;
	if(dp[l][r]!=-1)return(dp[l][r]);
  	int ret = 0;
  	if(abs(W[l]-W[r])<=1&&rec(l+1,r-1)==r-l-1)ret=r-l+1;
  	for(int m=l;m<r;m++)ret=max(ret,rec(l,m)+rec(m+1,r));
  	return (dp[l][r]=ret);
}
  
int main(){
  	while(1){
		cin>>N;
		if(N==0)break;
    	for(int i=0;i<N;i++)cin>>W[i];
    	memset(dp,-1,sizeof(dp));
		cout<<rec(0,N-1)<<endl;
  	}
}

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

int main(){
  int n;
  while(cin >> n, n){
    vector<int> w(n); rep(i,n) cin >> w[i];
    vector<vector<int>> dp(n+1,vector<int>(n+1));

    // 区間DP
    FOR(j,2,n+1){ // 区間長
      for(int i = 0; i+j <= n; ++i){ // 区間左端 dp[i][i+j]を決定する
	//printf("[%d,%d)\n",i,i+j);
	FOR(k,2,j-1) // dp[i][i+k],dp[i+k][i+j]を元に計算
	  dp[i][i+j] = max(dp[i][i+j], dp[i][i+k]+dp[i+k][i+j]);
	
	if(abs(w[i] - w[i+j-1]) <= 1)
	  dp[i][i+j] = max(dp[i][i+j], dp[i+1][i+j-1] + 2);
      }
    }

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