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;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

const int MAX_N = 1001;
bool dp[MAX_N][MAX_N];
int dp2[MAX_N];

int main()
{
    ios::sync_with_stdio(false);

    int N;
    while (cin >> N, N) {
        vector<int> W(N);
        rep(i, N) cin >> W[i];

        memset(dp, 0, sizeof(dp));
        memset(dp2, 0, sizeof(dp2));

        for (int i=0; i<N-1; i++) {
            if (abs(W[i] - W[i+1]) <= 1) {
                dp[i][i+1] = true;
            }
        }

        for (int len=2; len<=N; len += 2) {
            for (int l=0; l<=N-len; l++) {
                int r = l + len - 1;

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


                if (!dp[l][r]) continue;
                if (0 <= l-1 && r+1 < N && abs(W[l-1] - W[r+1]) <= 1) {
                    dp[l-1][r+1] = true;
                }
            }
        }

#ifdef DEBUG
        rep(i, N) {
            rep(j, N) {
                cerr << "xo"[dp[i][j]] << " ";
            }
            cerr << endl;
        }
#endif

        for (int r=1; r<=N; r++) {
            dp2[r] = max(dp2[r], dp2[r-1]);
            for (int l=0; l<r; l++) {
                if (dp[l][r-1]) {
                    int len = r - l;
                    dp2[r] = max(dp2[r], dp2[l] + len);
                }
            }
        }

#ifdef DEBUG
        rep(i, N+1) cerr << dp2[i] << " ";
        cerr << endl;
#endif

        cout << dp2[N] << endl;
    }
}

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

#define ll long long
using namespace std;
#define REP(i,j,n) for(int i = j; i < (n); i++)

void sol(int n) {
    vector<int> a(n);
    REP(i,0,n) cin >> a[i];

    int dp[n+1][n+1];
    REP(i,0,n+1) REP(j,0,n+1) dp[i][j] = 0;
    REP(j,1,n+1){
        REP(i,0,j){
            dp[i][j] = dp[i][j-1];
            if(i <= j-2) dp[i][j] = max(dp[i][j],dp[i][j-2]+(abs(a[j-1]-a[j-2])<=1)*2);
            REP(k,i,j){
                dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j-1]+(j-1 != k && (j-1)-(k+1) == dp[k+1][j-1] && abs(a[j-1]-a[k])<=1)*2);
            }
        }
    }

    cout << dp[0][n] << endl;

    return;
}

int main() {
    int n;
    
    while(cin >> n , n){
        sol(n);
    }
    return 0;
}

C++: #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main(){
  int n;
  while(cin>>n){
    if(n==0) return 0;
    
    vector<int> A(n+1);
    rep(i,n) cin>>A[i+1];
    
    int dp[310][310];
    rep(i,310){
      rep(j,310) dp[i][j]=0;
    }
    
    for(int i=1;i<n;i++){
      if(abs(A[i]-A[i+1])<=1){
        dp[i][i+1]=2;
      }
    }
    
    for(int l=3;l<=n;l++){
      for(int i=1;i+l-1<=n;i++){
        int j=i+l-1;
        if(dp[i+1][j-1]==j-i-1 && abs(A[i]-A[j])<=1) dp[i][j]=j-i+1;
        for(int k=i;k<j;k++){
          dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
        }
      }
    }
    cout<<dp[1][n]<<endl;
  }
    
  return 0;
}

C++: /* 
区間DPをします
F(l, r) := [l,r)の中で叩き出せる個数
*/

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

int dp[400][400];

// [l, r)
/*
int rec(int l, int r, vector<int> &w) {
    if (dp[l][r] != -1) return dp[l][r];
    if (abs(l-r)<=1) return 0;

    int res = 0;
    if (abs(w[l]-w[r-1]) <= 1 && rec(l+1,r-1,w) == r-l-2) res = r-l;
    for (int mid = l+1; mid < r; ++mid) res=max(res,rec(l,mid,w)+rec(mid,r,w));
    return dp[l][r] = res;
}
*/

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

int main(int argc, char const *argv[]) {
    int n;
    while(cin>>n,n) {
        vector<int> w(n); rep(i,n) cin>>w[i];
        memset(dp,0,sizeof(dp));
        // cout << rec(0,n,w) << endl;
        cout << calc(n,w) << endl;
    }
    return 0;
}