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>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9+7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct edge{
    ll to, cost;
};


ll gcd(ll a,ll b){
    if(b == 0){
        return a;
    }else{
        return gcd(b, a%b);
    }
}

ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll keta(ll N){
    int tmp{};
    while( N > 0 ){
        tmp += ( N % 10 );
        N /= 10;
    }
    N = tmp;
    return N;
}

// 回文
bool kai(string S){
    bool flag = true;
    for(ll i=0;i<S.size()/2;++i){
        if(S[i] != S[S.size()-i-1]){
            flag = false;
            break;
        }
    }
    return flag;
}
// ---------------------------------------------

int main(){
    while(true){
        ll n;
        cin>>n;
        if(n == 0){
            return 0;
        }
        vector<ll> w(n);
        for(ll i=0;i<n;++i){
            cin>>w[i];
        }

        vector<vector<ll>> dp(n+10, vector<ll> (n+10, 0));
        vector<vector<bool>> ok(n+10, vector<bool>(n+10, false));
        for(ll len=1;len<=n;++len){
            for(ll left=0;left<n-len;++left){

                ll right = left + len;
                for(ll mid=left;mid<right;++mid){
                    if(ok[left][mid] and ok[mid+1][right]){
                        ok[left][right] = true;
                    }
                }
                if(len == 1){
                    if(abs(w[left] - w[right]) <= 1){
                        ok[left][right] = true;
                    }
                }
                if(ok[left][right]){
                    ll x = left - 1;
                    ll y = right + 1;
                    while(true){
                        if(x < 0 or x >= n or y < 0 or y >= n)break;
                        if(abs(w[x] - w[y]) >= 2)break;
                        ok[x][y] = true;
                        x--;
                        y++;
                    }
                }
            }
        }

        for(ll i=0;i<n;++i){
            for(ll j=i+1;j<n;++j){
                if(ok[i][j]){
                    dp[i][j] = j - i + 1;
                }
            }
        }
        for(ll i=0;i<n;++i){
            for(ll j=i+1;j<n;++j){
                for(ll k=i;k<j;++k){
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);
                }
            }
        }
        ll ans = dp[0][n-1];
        cout<<ans<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1LL << 30;

ll n;
vll w(300);
vvll dp(350, vll(350, -1));
vll result(0);

void reset(){
  for(int i = 0; i < 350; i++){
    for(int j = 0; j < 350; j++){
      dp.at(i).at(j) = -1;
    }
  }
}

ll func(ll a, ll b){ //[a, b)間で叩き出せる上限
  if(dp.at(a).at(b) != -1) return dp.at(a).at(b);
  if(b-a == 1) return dp.at(a).at(b) = 0;
  if(b-a == 2){
    if(abs(w.at(a)-w.at(b-1)) <= 1) return dp.at(a).at(b) = 2;
    return 0;
  }

  if(func(a+1, b-1) == b-a-2 && abs(w.at(a)-w.at(b-1)) <= 1) return dp.at(a).at(b) = b-a;

  ll ans = 0;
  for(int i = a+1; i < b; i++){
    chmax(ans, func(a, i) + func(i, b));
  }
  return dp.at(a).at(b) = ans;
}

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

  reset();

  for(int i = 0; i < n; i++){
    cin >> w.at(i);
  }

  result.push_back(func(0, n));

}

for(auto x: result) cout << x << endl;

}

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

using namespace std;

int n;
vector<int> w;

int memo[301][301];

bool rec(int l, int r) {
  if (r == l) {
    return (memo[l][r] = false);
  }
  if (r - l == 1) {
    if (abs(w[l] - w[r]) <= 1) {
      return (memo[l][r] = true);
    } else {
      return (memo[l][r] = false);
    }
  }

  if (abs(w[l] - w[r]) <= 1) {
    if (rec(l + 1, r - 1)) {
      return (memo[l][r] = true);
    }
  }

  if (abs(w[l] - w[l + 1]) <= 1) {
    if (rec(l + 2, r)) {
      return (memo[l][r] = true);
    }
  }

  if (abs(w[r] - w[r - 1]) <= 1) {
    if (rec(l, r - 2)) {
      return (memo[l][r] = true);    
    }
  }

  return (memo[l][r] = false);
}


int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    memset(memo, 0, sizeof(memo));

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        rec(i, j);        
      }
    }

    
    int ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (memo[i][j] > 0) {
          ans = max(ans, j - i + 1);
        }
        for (int k = i + 1; k <= j; k++) {
          for (int l = k + 1; l <= j; l++) {
            if (memo[i][k] > 0 && memo[l][j] > 0) {
              ans = max(ans, (k - i + 1) + (j - l + 1));
            }
          }
        }
      }
    }

    cout << ans << endl;
  }


  return 0;
}