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 <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>

#include <limits.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

int solve(const vector<int> &as, const int n) {
    vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1));

    function<int(int, int)> rec = [&](int l, int r) -> int {
        // [l, r)
        if (dp[l][r] >= 0) {
            return dp[l][r];
        }

        int cand = 0;

        if (r - l <= 1) {
            // do nothing
        } else if (r - l == 2) {
            if (abs(as[l] - as[l + 1]) <= 1) {
                cand = 2;
            }
        } else {
            for (int i = l + 1; i < r; i++) {
                chmax(cand, rec(l, i) + rec(i, r));
            }

            if ((r - l) % 2 == 0 && rec(l + 1, r - 1) == (r - l - 2) && abs(as[l] - as[r - 1]) <= 1) {
                chmax(cand, rec(l + 1, r - 1) + 2);
            }
        }

        dp[l][r] = cand;
        return cand;
    };

    int ans = rec(0, n);

    // printf("table: \n");
    // for (int i = 0; i <= n; i++) {
    //     for (int j = 0; j <= n; j++) {
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    return ans;
}

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

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

        cout << solve(as, n) << endl;
    }

    return 0;
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int main() {
    int n;
    cin >> n;
    while (n != 0) {
            
        vector<int> w(n);
        rep(i, n) cin >> w[i];
        // dp[i][j] : iからj-1までのブロックから取れる最大値
        vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
        for (int k = 2; k <= n; k++) {
            for (int i = 0; i + k <= n; i++) {
                if (dp[i + 1][i + k - 1] == k - 2 && abs(w[i] - w[i + k - 1]) < 2) {
                    dp[i][i + k] = k;
                }
                for (int j = i; j <= i + k; j++) {
                    dp[i][i + k] = max(dp[i][i + k], dp[i][j] + dp[j][i + k]);
                }
            }
        }
        cout << dp[0][n] << "\n";
        cin >> n;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int main(){
     
    int n;
    int w[500];
    while(true){
        cin >> n;
        if(n == 0){
            break;
        }
         
        for(int i = 0; i < n; i++){
            cin >> w[i];
        }
         
        int DP[500][500] = {};
         
        for(int i = 2; i <= n; i += 2){
            for(int j = 0; j <= n - i; j++){
                DP[j][j + i - 1] = DP[j + 1][j + i - 2];
                if((w[j] - w[j + i - 1]) * (w[j] - w[j + i - 1]) <= 1 && DP[j + 1][j + i - 2] == (i - 2) / 2){
                    DP[j][j + i - 1]++;
                }
                for(int k = j + 1; k < j + i - 1; k += 2){
                    DP[j][j + i - 1] = max(DP[j][j + i - 1], DP[j][k] + DP[k + 1][j + i - 1]);
                }
            }
        }
         
        if(n % 2 == 1){
            cout << max(DP[0][n - 2], DP[1][n - 1]) * 2 << endl;
        }else{
            cout << DP[0][n - 1] * 2 << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

int w[300], dp[301][301];
signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    if(!n)break;

    rep(i, n)scanf("%d", w+i);
    rep(i, n + 1)rep(j, n + 1)dp[i][j] = 0;

    rep(i, n - 1)dp[i][i + 2] = (abs(w[i + 1] - w[i]) <= 1) * 2;

    rep(i, n + 1)rep(j, i){
      for(int k = j;k < i;++k)dp[j][i] = max(dp[j][i], dp[j][k] + dp[k][i]);
      if(i - j >= 2 && abs(w[i - 1] - w[j]) <= 1 && dp[j + 1][i - 1] == i - j - 2)
      dp[j][i] = i - j;
    }
    //rep(i, n + 1)rep(j, n + 1)if(dp[i][j] > 0)pri3(i, j, dp[i][j]);
    pri(dp[0][n]);

  }
  return 0;
}