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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 301


bool solve(){
  int n, w[SIZE];
  bool dp[SIZE][SIZE];

  scanf("%d", &n);
  if(n == 0) return 0;

  for(int i=0;i<n;i++) scanf("%d", w+i);
  
  memset(dp, 0, sizeof(dp));
  
  for(int i=0;i<n;i++) dp[i][i] = true;

  for(int x=2;x<=n;x+=2){
    for(int l=0;l+x<=n;l++){
      int r = l + x;
      
      // if(l >= 2 && abs(w[l-2] - w[l-1]) <= 1) dp[l-2][r] = true;
      // if(r <= n-2 && abs(w[r] - w[r+1]) <= 1) dp[l][r+2] = true;

      if(abs(w[l] - w[r-1]) <= 1) dp[l][r] |= dp[l+1][r-1];

      for(int i=l+1;i<r;i++){
        dp[l][r] |= dp[l][i] && dp[i][r];
      }
    }
  }

  int dp2[SIZE] = {};
  
  for(int i=0;i<n;i++){
    for(int j=i+2;j<=n;j++){
      if(dp[i][j]) dp2[j] = max(dp2[j], dp2[i] + (j-i));
    }
    dp2[i+1] = max(dp2[i+1], dp2[i]);
  }
  
  printf("%d\n", dp2[n]);
  return true;
}

int main(){
  while(solve());
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    for(;;){
        int n;
        cin >> n;
        if(n == 0)  return 0;
        vector<int> w(n);
        for(int i = 0; i < n; ++i)  cin >> w[i];
        vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1));
        auto MemoRec = [&](auto&& self, int l, int r) -> int {
            if(dp[l][r] != -1)  return dp[l][r];
            if(r - l <= 1)  return dp[l][r] = 0;
            if(r - l == 2){
                if(abs(w[l] - w[r - 1]) <= 1)   return dp[l][r] = 2;
                else    return dp[l][r] = 0;
            }
            if(abs(w[l] - w[r - 1]) <= 1 && self(self, l + 1, r - 1) == r - l - 2){
                return dp[l][r] = r - l;
            }
            int res = 0;
            for(int m = l + 1; m < r; ++m){
                chmax(res, self(self, l, m) + self(self, m, r));
            }
            return dp[l][r] = res;
        };
        cout << MemoRec(MemoRec, 0, n) << '\n';
    }
}

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

int n;
vector<int> w;
bool canTake[301][301];
int dp[301][301];

int solve(int l, int r){
    if(l >= r){
        return 0;
    }else if(canTake[l][r]){
        return r - l;
    }else if(dp[l][r] >= 0){
        return dp[l][r];
    }

    int res = max(solve(l + 1, r), solve(l, r - 1));
    for(int i = l + 1 ; i < r ; ++i){
        res = max(res, solve(l, i) + solve(i, r));
    }

    return dp[l][r] = res;
}

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

        fill(canTake[0], canTake[n + 1], false);

        for(int len = 2 ; len <= n ; len += 2){
            for(int i = 0 ; i <= n - len ; ++i){
                for(int j = i + 1 ; j < i + len ; ++j){
                    if(canTake[i][j] && canTake[j][i + len]){
                        canTake[i][i + len] = true;
                    }
                }
                if(len == 2 && abs(w[i] - w[i + 1]) <= 1){
                    canTake[i][i + 2] = true;
                }
                if(canTake[i][i + len]){
                    int l = i - 1, r = i + len;
                    while(0 <= l && r < n && abs(w[l] - w[r]) <= 1){
                        canTake[l][r + 1] = true;
                        --l;
                        ++r;
                    }
                }
            }
        }

        fill(dp[0], dp[n + 1], -1);

        int ans = solve(0, n);
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> iv; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1, 0 };
#define mp make_pair
#define pb push_back

int ans;

void dfs(iv E, int depth)
{
  
  bool flag = false;
  repi(i,1,E.size())
    {
      if(depth + E.size() <= ans)continue;
      if(abs(E[i]-E[i-1])>1)continue;
      flag = true;
      iv F;
      rep(j,E.size())
	{
	  if(j == i || j == i - 1)continue;
	  F.pb(E[j]);
	  //printf("%d" ,E[j]);
	}
      // printf("\n");
	  
      dfs(F, depth+2);
    }
  if(!flag)ans=max(ans,depth);
}
int main()
{
  while(true)
    {
      int n;
      scanf("%d", &n);
      if(n==0)break;

      iv W;
      rep(i,n)
	{
	  int w;
	  scanf("%d", &w);
	  W.pb(w);
	}
      ans = 0;
      dfs(W, 0);
      printf("%d\n", ans);
    }
  return 0;
}