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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

int N;
vector<int> w;

int solve() {
	//初期化
	//操作の対象は隣合う2マス
	//幅1や幅0の隙間は一つもとることができない。
	vector<vector<int>> dp(N + 1, vector<int>(N + 1, -N));
	for (int i = 0; i <= N; i++) {
		dp[i][i] = 0;
		if (i < N)dp[i][i + 1] = 0;
	}

	//幅が小さい方から更新するので、それより短い区間はすべて値が入ってる。
	//[l,r)なので右端の要素はr-1
	for (int bet = 2; bet <= N; bet++) {
		//左端
		for (int i = 0; i + bet <= N; i++) {
			int j = i + bet;

			//i,jの間が全部抜けてる状態ならi,jも抜けるかもしれない
			if (dp[i + 1][j - 1] == j - i - 2 && abs(w[i] - w[j - 1]) <= 1) {
					chmax(dp[i][j], j - i);
					continue;
			}
			
			//i,jの間の区切り方を全部試す
			for (int k = i + 1; k <= j - 1; k++) {
				chmax(dp[i][j], dp[i][k] + dp[k][j]);
			}
			

		}
	}

	return dp[0][N];
}

int main() {
	while (cin >> N,N) {
		w.resize(N);
		for (int i = 0; i < N; i++)cin >> w[i];
		cout << solve() << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,a[300];
main()
{
	while(cin>>n,n)
	{
		for(int i=0;i<n;i++)cin>>a[i];
		bool ok[301][301]={};
		for(int i=0;i<n-1;i++)
		{
			if(abs(a[i+1]-a[i])<=1)ok[i][i+2]=1;
		}
		for(int w=2;w<=n;w++)
		{
			for(int i=0;i+w<=n;i++)
			{
				for(int j=1;j<w;j++)
				{
					if(ok[i][i+j]&&ok[i+j][i+w])ok[i][i+w]=1;
				}
				if(!ok[i][i+w])continue;
				if(i>0&&i+w<n&&abs(a[i-1]-a[i+w])<=1)
				{
					ok[i-1][i+w+1]=1;
				}
			}
		}
		int dp[301][301]={};
		for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)if(ok[i][j])dp[i][j]=j-i;
		for(int i=0;i<n;i++)for(int j=i+1;j<=n;j++)
		{
			for(int k=i+1;k<j;k++)dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
		}
		cout<<dp[0][n]<<endl;
	}
}

C++: // written by @kakira9618
// problem: http://icpc.iisf.or.jp/past-icpc/domestic2016/problems/all_ja.html#section_D

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

int n;
vector<int> W;
int dp[301][301];

// O(n^3)
int dfs(int l, int r) {
    if (r - l <= 1) return dp[l][r] = 0;
    if (dp[l][r] != -1) return dp[l][r]; // ?????¢???
    int ans = 0;
    
    // i????????§??????????????????????¨?????????????????§£??????
    for(int i = l + 1; i < r - 1; i++) {
        ans = max(ans, dfs(l, i) + dfs(i, r));
    }
    
    // ?????£???2??????????????¶?????????1??\???????????????????????¨????¶??????????????????°?????£????????????????¶???????
    if (abs(W[r - 1] - W[l]) <= 1) {
        if (dfs(l + 1, r - 1) == r - l - 2) {
            ans = max(ans, dfs(l + 1, r - 1) + 2);
        }
    }
    return dp[l][r] = ans;
}

int main() {
    while(cin >> n, n) {
        memset(dp, -1, sizeof(int) * 301 * 301);
        W.clear();
        W.resize(n);
        for(int i = 0; i < n; i++) {
            cin >> W[i];
        }
        cout << dfs(0, n) << endl;
    }
    return 0;
}

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
int n;
int w[MAX];
map<string, int> wmap;

string nToStr(int n) {
	string tmp = "";
	for (int i = 0; i < n; i++) {
		tmp += "1";
	}
	return tmp;
}

int next(string s, int now) {
	for (int i = now+1; i < n; i++) {
		if (s[i] == '1') return i;
	}
	return n;
}

string pop(string s,int a,int b) { //w???a????????¨b?????????????????????
	s[a] = '0';
	s[b] = '0';
	return s;
}

int daruma(string s) {
	if (wmap.find(s) != wmap.end()) return wmap[s];
	

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

	return max;
}

int main() {
	cin >> n;
	while (n) {
		wmap.clear();
		for (int i = 0; i < n; i++) {
			cin >> w[i];
		}

		cout << daruma(nToStr(n)) << endl;

		cin >> n;
	}
	return 0;
}