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;
using ll = long long;
#define REP(iiii,nnnn) for (ll iiii=0,iiii##_len=(nnnn); iiii<iiii##_len;iiii++)
#define RANGE(i,is,ie) for (ll i=is,i##_end=(ie); i<=i##_end;i++)
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } else return false; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } else return false; }

int main() {

    for (ll n; cin >> n, n; ) {

        vector<ll> w(n);
        REP(i, n) cin >> w[i];
        vector<vector<ll>> dp(n, vector<ll>(n+1, 0));
        vector<vector<ll>> rm(n, vector<ll>(n+1, 0));
        REP(i, n) dp[i][i+1] = 0, rm[i][i+1] = w[i];
        RANGE(i, 2, n) {
            RANGE(l, 0, n-i) {
                ll r = l+i;
                RANGE(c, l+1, r-1) {
                    if (dp[l][c] == c-l && dp[c][r] == r-c) {
                        dp[l][r] = r-l;
                    } else if (dp[l][c] == c-l && dp[c][r] == r-c-1) {
                        dp[l][r] = c-l;
                        rm[l][r] = rm[c][r];
                    } else if (dp[c][r] == r-c && dp[l][c] == c-l-1) {
                        dp[l][r] = r-c;
                        rm[l][r] = rm[l][c];
                    } else if (dp[l][c] == c-l-1 && dp[c][r] == r-c-1 && abs(rm[l][c] - rm[c][r]) <= 1) {
                        dp[l][r] = r-l;
                    } else {
                        chmax(dp[l][r], dp[l][c] + dp[c][r]);
                    }
                }
            }
        }
        cout << dp[0][n] << endl;

    }

}

C++: #include<bits/stdc++.h>
using namespace std;
int N;
int W[305];
int dp[305][305];
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0){break;}
		for(int i=0;i<N;i++){
			scanf("%d",&W[i]);
		}
		for(int i=0;i<305;i++){
			for(int j=0;j<305;j++){
				dp[i][j]=0;
			}
		}
		for(int i=0;i<=N-2;i++){
			if(abs(W[i]-W[i+1])<=1){dp[i][1]=2;}
		}
		for(int j=3;j<=N-1;j+=2){
			for(int i=0;i+j<N;i++){
				//[i,i+j]
				if(abs(W[i+j-1]-W[i+j])<=1){
					dp[i][j]=max(dp[i][j],dp[i][j-2]+2);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i][j-2]);
				}
				if(abs(W[i]-W[i+1])<=1){
					dp[i][j]=max(dp[i][j],dp[i+2][j-2]+2);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i+2][j-2]);
				}
				if(abs(W[i]-W[i+j])<=1&&dp[i+1][j-2]==j-1){
					dp[i][j]=max(dp[i][j],j+1);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i+1][j-2]);
				}
			}
		}
		/*
		for(int i=150;i>=0;i-=2){
		printf("%d\n",dp[i][299-i]);
		}*/
		if(N%2==0){printf("%d\n",dp[0][N-1]);}
		else {printf("%d\n",max(dp[0][N-2],dp[1][N-2]));}
	}
	return 0;
}

C++: 
 // C
 #ifndef _GLIBCXX_NO_ASSERT
 #include <cassert>
 #endif
 #include <cctype>
 #include <cerrno>
 #include <cfloat>
 #include <ciso646>
 #include <climits>
 #include <clocale>
 #include <cmath>
 #include <csetjmp>
 #include <csignal>
 #include <cstdarg>
 #include <cstddef>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <ctime> 
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
 #include <ccomplex>
 #include <cfenv>
 #include <cinttypes>
 #include <cstdbool>
 #include <cstdint>
 #include <ctgmath>
 #include <cwchar>
 #include <cwctype>
 #endif
 
 // C++
 #include <algorithm>
 #include <bitset>
 #include <complex>
 #include <deque>
 #include <exception>
 #include <fstream>
 #include <functional>
 #include <iomanip>
 #include <ios>
 #include <iosfwd>
 #include <iostream>
 #include <istream>
 #include <iterator>
 #include <limits>
 #include <list>
 #include <locale>
 #include <map>
 #include <memory>
 #include <new>
 #include <numeric>
 #include <ostream>
 #include <queue>
 #include <set>
 #include <sstream>
 #include <stack>
 #include <stdexcept>
 #include <streambuf>
 #include <string>
 #include <typeinfo>
 #include <utility>
 #include <valarray>
 #include <vector> 
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
 #include <array>
 #include <atomic>
 #include <chrono>
 #include <condition_variable>
 #include <forward_list>
 #include <future>
 #include <initializer_list>
 #include <mutex>
 #include <random>
 #include <ratio>
 #include <regex>
 #include <system_error>
 #include <thread>
 #include <tuple>
 #include <typeindex>
 #include <type_traits>
 #include <unordered_map>
 #include <unordered_set>
 #endif
using namespace std;
int dp[310][310];

int main(){
    while(1){
        int n; cin >> n;
        if(!n) break;
        vector<int> w(n);
        for(int i=0;i<n;++i) cin >> w[i];
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                dp[i][j] = 0;
            }
        }
        //for(int i=0;i<n;++i) dp[i][i] = 0;

        for(long long W=2;W<=n;++W){
            for(int i=0;i<n;++i){
                int j = i+W-1;
                if(j>=n) continue;
                if(dp[i+1][j-1]==W-2 && abs(w[j] - w[i])<=1){
                    dp[i][j] = W;
                }
                for(int k=i;k<j;++k){
                    dp[i][j] = max(dp[i][k] + dp[k+1][j],dp[i][j]);
                }
            }
        }
        cout << dp[0][n-1] << endl;
    }
    return 0;
}