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 <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <cmath>

using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i,n) for(long long i=0; i<n; i++)
#define repr(i,n,m) for(long long i=m; i<n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) for (auto&& itr : x) { cerr << (itr) << " "; }

template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t;}
template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t;}

void can_take(int n, vector<vector<int>> &dp, vector<int> const& w) {
  repr(i, n, 0) repr(j, n-i, 0) {
    int left = j, right = i + j; // [left, right]
    int len = i + 1;

    repr(k, right, left) {
      if (dp[left][k] && dp[k+1][right]) dp[left][right] = true;
    }

    if (len == 2 && abs(w[left] - w[right]) < 2) dp[left][right] = true;

    if (dp[left][right]) {
      left --, right ++;
      while (left >= 0 && left < n && right >= 0 && right < n) {
        if (abs(w[left] - w[right]) < 2) dp[left][right] = true;
        else break;
        left --, right ++;
      }
    }
  }
}


int main() {
  ll n;

  while (cin >> n && n != 0) {
    vector<int> w(n);
    repr(i, n, 0) cin >> w[i];
    vector<vector<int>> dp1(n, vector<int>(n, 0));
    vector<vector<int>> dp2(n, vector<int>(n));

    can_take(n, dp2, w);
    /*for (auto it : dp2) {
      for (auto itr : it) {
        cerr << itr << " ";
      }
      cerr << endl;
    }*/

    repr(i, n, 0) repr(j, n, i+1) {
      if (dp2[i][j]) dp1[i][j] = j - i + 1;
    }

/*    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cerr << dp1[i][j] << " ";
      }
      cerr << endl;
    }*/

    repr(i, n, 0) repr(j, n, i) repr(k, j, i) {
      dp1[i][j] = max(dp1[i][j], dp1[i][k] + dp1[k+1][j]);
    }
    cout << dp1[0][n-1] << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int N;
int W[305];
int dp[305][305];
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0){break;}
		for(int i=0;i<N;i++){
			scanf("%d",&W[i]);
		}
		for(int i=0;i<305;i++){
			for(int j=0;j<305;j++){
				dp[i][j]=0;
			}
		}
		for(int i=0;i<=N-2;i++){
			if(abs(W[i]-W[i+1])<=1){dp[i][2]=2;}
		}
		for(int j=3;j<=N-1;j++){
			for(int i=0;i+j<=N;i++){
				//[i,i+j)
				for(int k=0;k<j;k++){
					dp[i][j]=max(dp[i][k]+dp[i+k][j-k],dp[i][j]);
				}
				if(abs(W[i]-W[i+j-1])<=1){
					dp[i][j]=max(dp[i][j],dp[i+1][j-2]+2);
				}
			}
		}
		/*
		for(int j=0;j<12;j++){
			for(int i=0;i+j<=12;i++){
				printf("%d ",dp[i][j]);
			}
			printf("\n");
		}*/
		/*
		for(int i=150;i>=0;i-=2){
		printf("%d\n",dp[i][299-i]);
		}*/
		printf("%d\n",dp[0][N]);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

int rec(int left = 0, int right = N - 1) {
    if (left == right) {
        return dp.at(left).at(right) = 0;
    }
    if (right - left == 1) {
        return dp.at(left).at(right) = (abs(w.at(left) - w.at(right)) <= 1) ? 2 : 0;
    }
    int &ret = dp.at(left).at(right);
    if (ret != -1) {
        return ret;
    }
    if (rec(left + 1, right - 1) == right - left - 1 && abs(w.at(left) - w.at(right)) <= 1) {
        return ret = right - left + 1;
    }
    for (int i = left; i < right; ++i) {
        ret = max(ret, rec(left, i) + rec(i + 1, right));
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (true) {
        cin >> N;
        if (N == 0) break;
        w.resize(N);
        for (int i = 0; i < N; ++i) {
            cin >> w.at(i);
        }

        dp.assign(N, vector<int>(N, -1));

        cout << rec() << endl;
    }

    return 0;
}