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 INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
int N;
bool ok[1001][1001];
int dp[1001][1001];
int ans[1001];
int main(){
    while(cin>>N,N){
       vector<int>v(N);
       REP(i,N)cin>>v[i];
       REP(i,1001)REP(j,1001){ok[i][j]=false;dp[i][j]=0;}
       //ok????????????
       /*REP(i,N){
           for(int j=i+1;j<N;j++){
               if(j-i==1&&abs(v[i]-v[j])<=1){ok[i][j]=true;dp[i][j]=2;continue;}
               if(j-i==2){ok[i][j]=false;dp[i][j]=0;continue;}
               for(int k=i+1;k+1<j;k++){
                   if(abs(v[i]-v[j])>1)continue;
                   if(ok[i][k]==false)continue;
                   if(ok[k+1][j]==false)continue;
                   ok[i][j]=true;
                   dp[i][j]=j-i+1;
               }
           }
       }*/
       for(int k=1;k<=N-1;k++){
          REP(i,N){
              if(i+k>=N)continue;
              if(k==1&&abs(v[i]-v[i+1])<=1){ok[i][i+1]=true;dp[i][i+1]=2;continue;}
              if(k==1)continue;
              if(k==2){ok[i][i+2]=false;dp[i][i+2]=0;continue;}
              if(abs(v[i]-v[i+k])<=1&&ok[i+1][i+k-1]==true){ok[i][i+k]=true;dp[i][i+k]=k+1;}
              for(int j=1;j<=k;j++){
                  if(ok[i][i+j]&&ok[i+j+1][i+k]){ok[i][i+k]=true;dp[i][i+k]=k+1;}
                  if(abs(v[i]-v[i+j]>1))continue;
                  if(ok[i][i+j-1]==false)continue;
                  if(ok[i+j+1][i+k]==false)continue;
                  ok[i][i+k]=true;
                  dp[i][i+k]=k+1;
              }
          }
       }
       REP(i,1001){
           ans[i]=0;
       }
       for(int i=1;i<N;i++){
           ans[i]=max(ans[i],ans[i-1]);
           ans[i]=max(ans[i],dp[0][i]);
           for(int j=1;j<i;j++){
               ans[i]=max(ans[i],ans[j]+dp[j+1][i]);
           }
       }
       //REP(i,N){REP(j,N)cout<<dp[i][j]<<" ";cout<<endl;}
       //REP(i,N)cout<<ans[i]<<endl;
       cout<<ans[N-1]<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cout<<(#v)<<":";for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;
#define printVS(vs) cout<<(#vs)<<":"<<endl;for(auto(s):(vs)){cout<<(s)<< endl;}
#define printVV(vv) cout<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;}
#define printP(p) cout<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cout<<(#vp)<<":"<<endl;for(auto(p):(vp)){cout<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cout << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cout << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 2e9;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

int n;
int a[301];
int dp[301][301];

int dfs(int l, int r) {
    if (l >= r) return 0;
    int &ret = dp[l][r];
    if (ret != -1) return ret;
    ret = 0;
    rep2(i, l + 1, r) {
        chmax(ret, dfs(l, i) + dfs(i, r));
    }
    if (l < r - 1 && abs(a[l] - a[r - 1]) <= 1 && dfs(l + 1, r - 1) == (r - 1) - (l + 1)) {
        chmax(ret, r - l);
    }
    return ret;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    while (cin >> n, n) {
        rep(i, n) cin >> a[i];
        rep(i, n + 1) rep(j, n + 1) dp[i][j] = -1;
        cout << dfs(0, n) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    cout << fixed << setprecision(10);
    
    int n;
    while(cin >> n) {
        if (n == 0) break;
        vector<int> w(n);
        rep(i, n) cin >> w[i];

        vector<vector<int>> dp(n, vector<int>(n));

        repr(b, 2, n+1) {
            rep(i, n-b+1) {
                int j = i + b - 1;

                if (dp[i+1][j-1] == b-2 && abs(w[i] - w[j]) < 2) {
                    dp[i][j] = b;
                } else {
                    repr(k, i, j) {
                        dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);
                    }
                }
            }
        }
        cout << dp[0][n-1] << endl;
    }
    return 0;
}
/*
4
1 2 3 4
4
1 2 3 1
5
5 1 2 3 6
14
8 7 1 4 3 5 4 1 6 8 10 4 6 5
5
1 3 5 1 3
0
*/

C++: #include <iostream>
#include <cstring>

constexpr int NUM = 300;
int w[NUM] = { 0 };
bool dp[NUM] = {false}; //[???][???] = ?????¨??????????????????

bool isDropedBetween(int a, int b) {
	if ((b - a) <= 1) return true;
	for (int i = a + 1; i < b; i++) if (!dp[i]) return false;
	return true;
}

bool isDroped(int a, int b) {
	for (int i = a; i <= b; i++) if (!dp[i]) return false;
	return true;
}

int main() {
	int n;
	while (std::cin >> n && n) {
		std::memset(dp, 0, sizeof(bool) * NUM);
		for (int i = 0; i < n; i++) std::cin >> w[i];

		for (int d = 1; d < n; d += 2) { //?????¨?????????
			for (int i = 0; i < n - d; i++) { //?????¨?????´???
				bool mightDrop = false;
				if (isDropedBetween(i, i + d)) mightDrop = true; //???????????????????????¨????????¨??????
				if (isDroped(i, i + d) || (mightDrop && std::abs(w[i] - w[i + d]) <= 1)) {
					for (int j = 0; j <= d; j++) dp[i + j] = true;
				}
			}
		}
		int count[2] = {0};
		for (int i = 0; i < NUM; i++) {
			if (dp[i]) count[1]++;
			else {
				count[0] += count[1] / 2 * 2;
				count[1] = 0;
			}
		}
		std::cout << count[0] << std::endl;
	}
	return 0;
}