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;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for(LL i = 0; i < n; ++i)
#define FOR(i, a, n) for(LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

//#define int long long

/*************** using variables ***************/
/**********************************************/

int solve(int n, vector<int> w){
    bool dp[305][305];
    LL dp2[305];
    
    REP(i, 305){
        dp2[i] = 0;
        REP(j, 305){
            dp[i][j] = false;
        }
    }

    for(int i = 1; i < n; i++){
        if(abs(w[i-1]-w[i]) <= 1) dp[i-1][i] = true;
    }

    for(int len = 4; len <= n; len += 2){
        REP(l, n){
            int r = l + len - 1;
            if(r >= n) break;

            for(int c = l+2; c < r; c += 2){
                if(dp[l][c-1] && dp[c][r]) dp[l][r] = true;
            }
            
            if(abs(w[l]-w[r]) <= 1 && dp[l+1][r-1]){
                dp[l][r] = true;
            }
        }
    }

    FOR(r, 1, n){
        dp2[r] = dp2[r-1];
        REP(l, r){
            if(dp[l][r]){
                if(l == 0) dp2[r] = max(dp2[r], r - l + 1);
                else dp2[r] = max(dp2[r], dp2[l-1] + (r - l + 1));
            }
        }
    }
    return dp2[n-1];
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n;
    while(cin >> n, n){
        vector<int> w(n, 0);   
        REP(i, n) cin >> w[i];
        cout << solve(n, w) << endl;
    }
   
}

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

vector<int>as;
bool ok(int a,int b){
  return abs(as[a]-as[b])<=1;
}

int main() {
  while(1){
    
    int N;cin>>N;
    if(!N)break;
    as.resize(N);
    vector<vector<int>>dp(N+1,vector<int>(N+1,-100000));
    for(int i=0;i<N;++i){
      int a;cin>>a;as[i]=a;
    }
    for(int i=0;i<=N;++i){
      dp[i][i]=0;
    }
    for(int t=2;t<=N;++t){
      for(int l=0;l<=N-t;++l){
	const int r=l+t;
	dp[l][r]=max(dp[l+1][r],dp[l][r-1]);
	if(ok(l,l+1))dp[l][r]=max(dp[l+2][r]+2,dp[l][r]);
	if(ok(r-2,r-1))dp[l][r]=max(dp[l][r-2]+2,dp[l][r]);
	if(ok(l,r-1)){
	  if(dp[l+1][r-1]==(r-l-2)){
	    dp[l][r]=max(dp[l+1][r-1]+2,dp[l][r]);
	  }
	}
      } 
    }

    int ans=dp[0][N];
    ans=max(0,ans);
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

inline int abs(int a, int b)
{
    return a<b ? b-a : a-b;
}

int w[301];
bool rm[301][301];
int rmct[301][301];

int main()
{
    int n;
    while(cin >> n, n)
    {
        for(int i=0; i<n; i++) cin >> w[i];
        for(int i=0; i<=n; i++)
            for(int j=0; j<=n; j++)
                rm[i][j] = i==j;
        for(int r=2; r<=n; r+=2)
        {
            for(int i=0; i+r<=n; i++)
            {
                for(int s=2; s<r; s+=2)
                {
                    if(rm[i][i+s] && rm[i+s][i+r]) rm[i][i+r] = true;
                }
                if(!rm[i][i+r] && rm[i+1][i+r-1] && abs(w[i], w[i+r-1])<=1)
                    rm[i][i+r] = true;
            }
        }
        /*
        int rmw = -1;
        for(int rmr = n / 2 * 2; rmw==-1 && rmr >= 0; rmr-=2)
        {
            for(int i=0; i+rmr<=n; i++)
            {
                if(rm[i][i+rmr])
                {
                    rmw = rmr;
                    break;
                }
            }
        }
        */
        for(int y=0; y<=n; y++)
            for(int x=0; x<=n; x++)
                rmct[y][x] = rm[y][x] ? x - y : 0;
        for(int r=1; r<=n; r++)
        {
            for(int i=0; i<=n; i++)
            {
                for(int s=1; s<r; s++)
                {
                    rmct[i][i+r] = max(rmct[i][i+r], rmct[i][i+s]+rmct[i+s][i+r]);
                }
            }
        }
        cout << rmct[0][n] << endl;
        // XXX DEBUG
        /*
        printf("i\\j ");
        for(int x=0; x<=n; x++) printf("%3d ", x);
        cout << endl;
        for(int y=0; y<=n; y++)
        {
            printf("%3d ", y);
            for(int x=0; x<=n; x++)
            {
                printf("%3d ", rm[y][x]);
            }
            cout << endl;
        }
        */
        // XXX DEBUG
        //cout << rmw << endl;
    }
}

C++: #include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> LLP;

// const int INF = (1 << 30) - 1;
// const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;
// const double PI = M_PI;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (true) {
    int N;
    cin >> N;
    if (N == 0) {
      break;
    }
    vector<int> w(N);
    rep(i, N) { cin >> w[i]; }
    int dp[N][N + 1]; // [l, r)
    rep(i, N) { fill(dp[i], dp[i] + N + 1, 0); }

    for (int W = 2; W <= N; W++) {
      for (int l = 0; l < N; l++) {
        int r = l + W;
        if (r > N) {
          continue;
        }
        if (dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r - 1]) <= 1) {
          dp[l][r] = dp[l + 1][r - 1] + 2;
        }
        for (int k = l + 1; k < r; k++) {
          dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r]);
        }
      }
    }

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