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 MAX_N 40
#define INF 1000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int w[300];
bool dp[300][300];
int ddp[301];

void solve(){
    memset(dp,0,sizeof(dp));
    rep(i,n) cin >> w[i];
    for(int i = 1; i < n; i+= 2){
        for(int j = 0; j < n-i; j++){
            if(abs(w[j]-w[j+i]) <= 1){
                if(i == 1 || dp[j+1][j+i-1] == true){
                    dp[j][j+i] = true;
                }
            }
            for(int k = j+1; k <= j+i-2; k += 2){
                if(dp[j][k] == true && dp[k+1][j+i] == true){
                    dp[j][j+i] = true;
                }
            }
        }
    }
    rep(i,n) ddp[i] = 0;
    for(int i = 1; i < n; i += 2) if(dp[0][i]) ddp[i] = i+1;
    for(int i = 1; i < n; i++){
        ddp[i] = max(ddp[i],ddp[i-1]);
        for(int j = 1; j < n-i; j += 2){
            if(dp[i][i+j]) ddp[i+j] = max(ddp[i+j],ddp[i-1]+j+1);
        }
    }
    cout << ddp[n-1] << endl;
}

int main(){
    while(cin >> n){
        if(n == 0) break;
        solve();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for(int i=0; i<(a); i++)
typedef long long ll;
const ll inf = 1LL<<60;

int main()
{
  vector<ll> ans;

  while(1) {
    int n; cin >> n;
    if (!n) break;
    vector<ll> w(n, 0);
    rep(i, n) cin >> w[i];
    vector<vector<ll>> dp(n+1, vector<ll>(n+1, 0));
    for (int len=2; len<=n; len++) {
      for (int l=0; l+len<=n; l++) {
        int r = l+len;
        if (dp[l+1][r-1] == len-2 && abs(w[l]-w[r-1]) <= 1) {
          dp[l][r] = len;
        }
        for (int m=l; m<=r; m++) {
          dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
        }
      }
    }
    ans.push_back(dp[0][n]);
  }

  for (auto v:ans) cout << v << endl;

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<tuple>
#include<algorithm>
#include<cmath>
using namespace std;
vector<tuple<int, int, int>>vec; int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; vec.clear(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; } for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { vec.push_back(make_tuple(j - i, i, j)); } }
		sort(vec.begin(), vec.end());
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 1; } }
		for (int i = 0; i < vec.size(); i++) {
			int a1 = get<1>(vec[i]), a2 = get<2>(vec[i]);
			if (dp[a1][a2] == 0)continue;
			if (a2 < n - 2 && abs(a[a2 + 1] - a[a2 + 2]) <= 1)dp[a1][a2 + 2] = 1;
			if (a1 > 0 && a2 < n - 1 && abs(a[a1 - 1] - a[a2 + 1]) <= 1)dp[a1 - 1][a2 + 1] = 1;
		}
		for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dp[i][j] == 1)dp[i][j] = j - i + 1; } }
		for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { for (int k = 0; k < n; k++) { dp[i][k] = max(dp[i][k], dp[i][j - 1] + dp[j][k]); } } }
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}

C++: // finish date: 2018/07/03
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <climits>
#include <functional>

using namespace std;

#define FOR(i, a, b) for(int (i)=a;(i)<(b);(i)++)
#define rep(i, n)  FOR(i,0,n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<long double> vd;
typedef vector<vector<long double>> vvd;
typedef vector<vector<vector<long double>>> vvvd;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> pii;
const int bigmod = 1000000007;
const int INF = 1050000000;


int main() {
    int n;

    while(true) {
        cin >> n;
        if(n == 0) {
            break;
        }

        vi w(n);
        rep(i, n) {
            cin >> w[i];
        }

        vvi dp(n + 1, vi(n + 1, 0));

        rep(i, n - 1) {
            dp[i][i + 1] = (abs(w[i] - w[i + 1]) <= 1) ? 2 : 0;
        }

        FOR(k, 2, n) {
            rep(i, n) {
                int j=i+k;
                if(j >= n) {
                    continue;
                }
                if(abs(w[i] - w[j]) <=1 && dp[i + 1][j - 1] == (j - 1) - (i + 1) +1) {
                    dp[i][j] = j - i + 1;
                } else {
                    FOR(l, i, j) {
                        dp[i][j] = max(dp[i][j], dp[i][l] + dp[l + 1][j]);
                    }
                }
            }
        }

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX_N = 310;
//????????????
int dp[MAX_N][MAX_N];

int N;
vector<int> W;

void solve(){
    for(int i=0; i<N-1; i++)
        if(abs(W[i]-W[i+1]) <= 1)
            dp[i][i+2] = 2;
    
    for(int l=3; l<=N; l++){
        for(int i=0; i<=N-l; i++){
            //segmentation
            for(int j=i; j<i+l; j++)
                dp[i][i+l] = max(dp[i][i+l], dp[i][j] + dp[j][i+l]);
            //expansion
            if(dp[i+1][i+l-1] == l-2 && abs(W[i]-W[i+l-1]) <= 1)
                dp[i][i+l] = l;
        }
    }
    
    cout << dp[0][N] << endl;
}

int main(){
    while(cin >> N, N){
        W.resize(N);
        for(auto &v: W)
            cin >> v;
        
        memset(dp, 0, sizeof(dp));
        solve();
    }
    return 0;
}