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"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

ll solve(ll n){
    ll res = 0;
    vector<ll> w(n); for(auto &in:w) cin >> in;
    vector<vector<ll>> dp(n+1,vector<ll>(n+1,0));
    for(int i = 2; i <= n;i++){
        for(int j = 0; j < n;j++){
            int k = j + i -1;
            if(k >= n) continue;
            
            if(dp[j+1][k-1] == i-2 && abs(w[j] - w[k]) <= 1) dp[j][k] = i;
            for(int l = j; l < k;l++){
                dp[j][k] = max(dp[j][k],dp[j][l] + dp[l+1][k]);
            }
        }
    }
    res = dp[0][n-1];
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    ll n;
    while(cin >> n,n){
        cout << solve(n) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

int main(void) {
  int N;
  while (cin >> N, N) {
    vector<int> W(N);
    for (int i = 0; i < N; i++) cin >> W[i];

    int dp[300][300] = {};
    for (int l = 0; l + 1 < N; l++) {
      if (abs(W[l] - W[l+1]) > 1) continue;
      dp[l][l+1] = 2;
    }

    for (int d = 3; d <= N; d++) {
      for (int l = 0; l + d <= N; l++) {
        int r = l + d - 1;
        if (abs(W[l] - W[r]) <= 1 && dp[l+1][r-1] == r - l - 1)
          dp[l][r] = dp[l+1][r-1] + 2;

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

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

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define WRAP(y,x,h,w) (0<=y && y<h && 0<=x && x<w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1,0,1,0 }; const int dy[] = { 0,1,0,-1 };

int main() {
	int n;
	while (cin >> n, n) {
		vi num(n);
		REP(i, n) {
			cin >> num[i];
		}
		vvi dp(n, vi(n, 0));
		FOR(i, 2, n + 1) {
			if (i == 2) {
				REP(j, n - 1)
					if (abs(num[j] - num[j + 1]) <= 1) {
						dp[j][j + 1] = 2;
					}
			}
			else {
				REP(j, n - (i - 1)) {
					REP(k, i - 1) {
						dp[j][j + i - 1] = max(dp[j][j + i - 1], dp[j][j + k] + dp[j + k + 1][j + i - 1]);
					}
					if ((i % 2 == 0) && (dp[j + 1][j + i - 2] == i - 2) && (abs(num[j] - num[j + i - 1]) <= 1))
						dp[j][j + i - 1] = i;
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

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

using namespace std;
typedef long long int ll;

int main()
{
	while(1){
		int n;
		scanf("%d", &n);
		if(n==0) return 0;
		int w[300];
		for(int i=0; i<n; i++){
			scanf("%d", &w[i]);
		}
		if(n==1){
			printf("%d\n", 0);
			continue;
		}
		int ans[301][301];
		for(int i=0; i<=n; i++){
			ans[i][i]=0;
			if(i+1<=n) ans[i][i+1]=0;
		}
		for(int d=2; d<=n; d++){
			for(int i=0; i+d<=n; i++){
				ans[i][i+d]=ans[i+1][i+d];
				for(int j=i+1; j<i+d; j++){
					if(abs(w[i]-w[j])<=1 && 2*ans[i+1][j]==j-i-1){
						if(ans[i][i+d]<ans[i+1][j]+1+ans[j+1][i+d]) ans[i][i+d]=ans[i+1][j]+1+ans[j+1][i+d];
					}
				}
			}
		}
		printf("%d\n", 2*ans[0][n]);
	}
	return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1611&lang=jp
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int n;
    while(cin >> n, n)
    {
        int dp[305][305] = {};
        vector<int> w(n);

        for(auto& _w : w)
        {
            cin >> _w;
        }

        for(int b = 2; b <= n; b++)
        for(int l = 0; l < n; l++)
        {
            int r = l + b - 1;
                if( r >= n )break;

                if( abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == r - l - 1)
                {
                    dp[l][r] = r - l + 1;
                }
                else
                for(int k = l; k < r; k++)                
                {
                    dp[l][r] = max( dp[l][r], dp[l][k] + dp[k + 1][r] );
                }
        }
        cout << dp[0][n - 1] << endl;
    }
    return 0;
}