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 FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111
	
	int dp[3100][3100];

	
	void solve(){


		while(1){
		ini(n);
		if(n == 0)return;

		FOR(i,0,n+1)FOR(i1,0,n+1)dp[i][i1] = 0;
		vi morty(n);
		FOR(i,0,n){
			in(morty[i]);
		}
		FOR(i,2,n+1){
			FOR(i1,0,n){
				int l = i1;
				int r = i1+i;
				if(r>n)continue;
				
				if(abs(morty[l]-morty[r-1])<=1 and r-l-2 == dp[l+1][r-1])dp[l][r] = dp[l+1][r-1]+2;

				else{
					FOR(x,l+1,r){
						dp[l][r] = max(dp[l][r],dp[l][x]+dp[x][r]);
					}
				}
			}
		}
		out(dp[0][n]);
		}
		

		


		
		
	}























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}

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

using namespace std;

using i64 = int64_t;
const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

bool solve(){
    int n;
    cin >> n;
    if(!n)
        return 0;
    vector<int> w(n);
    for(auto& x : w)
        cin >> x;

    vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, 0));
    for(i64 i = 0; i <= n; ++i)
        dp[i][i] = 1;
    for(int i = 0; i + 2 <= n; ++i)
        dp[i][i + 2] = (abs(w[i] - w[i + 1]) <= 1);

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

    /*
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j)
            cout << dp[i][j];
        cout << endl;
    }
    */

    vector<i64> dp2(n + 1, INF);
    dp2[0] = 0;
    for(int i = 0; i < n; ++i){
        dp2[i + 1] = min(dp2[i + 1], dp2[i] + 1);
        for(int j = i + 1; j <= n; ++j)
            if(dp[i][j])
                dp2[j] = min(dp2[j], dp2[i]);
    }

    cout << n - dp2[n] << endl;

    return true;
}

signed main(){
    while(solve());
}

C++: #include <bits/stdc++.h>
#define loop for(;;)
typedef std::vector<long> LI;
template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
template<> void scan1(std::string& x) { if( not ( std::cin >> x ) ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

struct Solver {
   long memo[314][314];
   LI xs;
   long dfs(long left, long right) {
      if( memo[left][right] != -1 ) return memo[left][right];
      if( right - left <= 1 ) return 0;
      long res = 0;
      if( std::abs(xs[right-1] - xs[left]) <= 1 ) {
         long x = dfs(left + 1, right - 1);
         if( x == (right - 1) - (left + 1) ) {
            res = std::max(res, x + 2);
         }
      }
      for(long i = left + 1; i <= right - 1; ++i) {
         res = std::max(res, dfs(left, i) + dfs(i, right));
      }
      return memo[left][right] = res;
   }
   void solve() {
      long n; scan(n);
      if( n == 0 ) exit(0);
      xs.resize(n); for(long &x : xs) scan(x);
      init();
      printf("%ld\n", dfs(0, n));
   }
   void init() {
      for(long i = 0; i < 314; ++i) {
         for(long k = 0; k < 314; ++k) {
            memo[i][k] = -1;
         }
      }
   }
};

int main() {
   loop std::shared_ptr<Solver>(new Solver())->solve();
}

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

vector<int> w;
int memo[300][300];

int calcMax(int l, int r){

    if(r < l){
        return 0;
    }

    int temp = memo[l][r];
    if(temp != -1){
        return temp;
    }

    if(r == l){
        memo[l][r] = 0;
        return 0;
    }

    if(r - l == 1){
        if(abs(w[l] - w[r]) <= 1){
            memo[l][r] = 2;
            return 2;
        }
        else{
            memo[l][r] = 0;
            return 0;
        }
    }

    int ret = 0;
    if(abs(w[l] - w[r]) <= 1 && calcMax(l + 1, r - 1) == (r - l - 1)){
        ret = max(ret, calcMax(l + 1, r - 1) + 2);
    }

    for(int i = l; i < r; i++){
        ret = max(ret, calcMax(l, i) + calcMax(i + 1, r));
    }

    /* for(int i = 1; i <= 2; i++){
        ret = max(ret, calcMax(l, r - i) + calcMax(r - i + 1, r));
    }*/

    memo[l][r] = ret;
    return ret;
}

int main(){

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

        memset(memo, -1, sizeof(memo));

        int ans = calcMax(0, n - 1);

        cout << ans << endl;
    }

    return 0;
}