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<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back

void Yes(){
	cout<<"Yes"<<endl;
	exit(0);
}
 
void No(){
	cout<<"No"<<endl;
	exit(0);
}

int dp[330][330];
int n;
int w[330];

int rec(int l, int r) {
    if(dp[l][r] != -1) return dp[l][r];
    if(abs(r - l) <= 1) return 0;

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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
    for(;;) {
        cin >> n;
        if(n == 0) break;
        rep(i, n) cin >> w[i];
        rep(i, n + 1) rep(j, n + 1) dp[i][j] = -1;
        cout << rec(0, n) << endl;
    }
	return 0;
}

C++: #include <array>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>

int blocks[300];
int8_t strike_range[300][300];
int score_in_range[300][300];

const int8_t STRIKE_RANGE_UNDEF = 0;
const int8_t STRIKE_RANGE_NO = 1;
const int8_t STRIKE_RANGE_YES = 2;

bool can_strike(int x, int y) {
	return std::abs(blocks[x] - blocks[y]) <= 1;
}

int8_t search_range(int start, int end) {
	if (start >= end) return STRIKE_RANGE_UNDEF;
	if ((end - start + 1) % 2 != 0) return STRIKE_RANGE_NO;

	int8_t* p = &strike_range[start][end];
	if (*p != STRIKE_RANGE_UNDEF) return *p;

	if (start + 1 == end)
		return *p = can_strike(start, end) ? STRIKE_RANGE_YES : STRIKE_RANGE_NO;

	for (int i = start + 1; i < end - 1; i++) {
		if (search_range(start, i) == STRIKE_RANGE_YES
			&& search_range(i + 1, end) == STRIKE_RANGE_YES)
		{
			return *p = STRIKE_RANGE_YES;
		}
	}

	auto result = search_range(start + 1, end - 1);
	return *p = result == STRIKE_RANGE_YES && can_strike(start, end)
		? STRIKE_RANGE_YES : STRIKE_RANGE_NO;
}

int main()
{
	while (true) {
		int n;
		std::cin >> n;

		if (n == 0) return 0;

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

		std::memset(strike_range, 0, sizeof(strike_range));

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				score_in_range[i][j] = search_range(i, j) == STRIKE_RANGE_YES ? j - i + 1 : 0;
			}
		}

		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				for (int k = i; k < j; k++) {
					score_in_range[i][j] = std::max(
						score_in_range[i][j],
						score_in_range[i][k] + score_in_range[k + 1][j]
					);
				}
			}
		}

		std::cout << score_in_range[0][n - 1] << std::endl;
	}
}

C++: #define _GLIBCXX_DEBUG

#include <bits/stdc++.h>


using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = { 0 };
vector<int> w(n);

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

  int ret = 0;
  bool isOK = false;
  
  if (abs(w[l] - w[r - 1]) <= 1) {
    isOK = true;
  }


  for (int t = l + 1; t < r - 1; t++) {
    for (int k = t + 1; k < r; k++) {
      int temp = rec(t, k) + rec(k, r);
      ret = max(ret, ((isOK && temp == r - l - 2)? 2 : 0) + temp);   
    }
  }

  for (int t = l + 1; t < r - 1; t++) {
    int temp = rec(l, t) + rec(t, r);
    ret = max(ret, ((isOK && temp == r - l)? 2 : 0) + temp);
  }

  return ret;
}

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

    w.clear();
    w.resize(n);

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

    cout << rec(0, n) << endl;

  }

  

  return 0;
}