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 rep(i,a,b) for(int i=a;i<b;i++)




int N;
int W[300];
//-----------------------------------------------------------------
int dp1[300][300];
int dp2[300];
int sol() {
	// clear
	rep(i, 0, 300) rep(j, 0, 300) dp1[i][j] = false;
	rep(i, 0, 300) dp2[i] = 0;

	// dp1
	rep(L, 0, N - 1) {
		int R = L + 1;
		if (abs(W[L] - W[R]) <= 1) dp1[L][R] = true;
	}
	for (int len = 4; len <= N; len += 2) rep(L, 0, N) {
		int R = L + len - 1;
		if (N <= R) break;

		// ok + ok
		for (int C = L + 2; C < R; C += 2) if (dp1[L][C - 1] && dp1[C][R]) dp1[L][R] = true;
		// ? + ok + ?
		if (dp1[L + 1][R - 1]) if (abs(W[L] - W[R]) <= 1) dp1[L][R] = true;
	}

	// dp2
	rep(R, 1, N) {
		dp2[R] = max(dp2[R], dp2[R - 1]);
		rep(L, 0, R) {
			if (dp1[L][R]) {
				if (L == 0)
					dp2[R] = max(dp2[R], R - L + 1);
				else
					dp2[R] = max(dp2[R], dp2[L - 1] + R - L + 1);
			}
		}
	}

	return dp2[N - 1];
}
//-----------------------------------------------------------------
int main() {
	while (cin >> N) {
		if (N == 0) return 0;
		rep(i, 0, N) scanf("%d", &W[i]);
		cout << sol() << endl;
	}
}

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

int main() {
    for(;;) {
        int n;
        std::cin >> n;
        if (n == 0) {
            break;
        }
        std::vector<int> xs(n);
        for(int i = 0; i < n; ++i) {
            std::cin >> xs[i];
        }
        int dp[310][310] = {{}};
        for(int w = 2; w <= n; ++w) {
            for(int l = 0; l <= n-w; ++l) {
                int r = l + w;
                // sep
                for(int k = l+1; k < r; ++k) {
                    dp[l][r] = std::max(dp[l][r], dp[l][k] + dp[k][r]);
                }
                // 2
                if (std::abs(xs[l] - xs[r-1]) <= 1 && dp[l+1][r-1] == r-l-2) {
                    dp[l][r] = std::max(dp[l][r], dp[l+1][r-1] + 2);
                }
            }
        }
        printf("%d\n", dp[0][n]);
    }
}

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

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

template<typename T>
static inline void chmin(T & ref, const T  value) {
    if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T & ref, const T value) {
    if (ref < value) ref = value;
}

int main() {
    vector<ll> ans;
    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        vector<ll> ps(n);
        rep(i, n) cin >> ps[i];

        vector<vector<ll>> dp(n, vector<ll>(n, 0));
        for (int i = 0; i < n - 1; i++) {
            if (abs(ps[i] - ps[i + 1]) < 2) {
                dp[i][i + 1] = 2;
            }
        }
        for (int w = 4; w <= n; w += 2) {
            for (int i = 0; i + w - 1 < n; i++) {
                for (int split = 2; split < w; split += 2) {
                    if (dp[i][i + split - 1] == split && dp[i + split][i + w - 1] == w-split) {
                        dp[i][i + w - 1] = w;
                    }
                    if (dp[i][i + w - split - 1] == w - split && dp[i + w - split][i + w - 1] == split) {
                        dp[i][i + w - 1] = w;
                    }
                }
                if (dp[i + 1][i + w - 2] == w - 2) {
                    if (abs(ps[i] - ps[i + w - 1]) < 2) {
                        dp[i][i + w - 1] = w;
                    }
                }
            }
        }
        vector<vector<ll>> dp2 = dp;
        for (int length = 1; length < n; length++) {
            for (int i = 0; i + length < n; i++) {
                for (int j = i; j +1<= i+length; j++) {
                    chmax(dp2[i][i + length], dp2[i][j] + dp2[j + 1][i + length]);
                }
            }
        }
        ans.push_back(dp2[0][n - 1]);
    }
    for (int index = 0; index < ans.size();index++) {
        cout << ans[index] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int n;
int w[333];
int dp[333][333];

int rec(int le, int ri) {
    if (~dp[le][ri]) return dp[le][ri];
    if (le == ri) {
        return dp[le][ri] = 0;
    }
    if (ri - le + 1 == 2) {
        return dp[le][ri] = (abs(w[le] - w[ri]) <= 1 ? 2 : 0);
    }
    if (abs(w[le] - w[ri]) <= 1) {
        if (ri - 1 >= le + 1) {
            int t = (ri - 1) - (le + 1) + 1;
            dp[le][ri] = (rec(le + 1, ri - 1) == t ? rec(le + 1, ri - 1) + 2 : dp[le][ri]);
        }
    }
    for (int md = le + 1; md <= ri; md++) {
        dp[le][ri] = max(dp[le][ri], rec(le, md - 1) + rec(md, ri));
    }
    return dp[le][ri];
}

int main() {
    while (cin >> n && n) {
        for (int i = 0; i < n; i++) cin >> w[i];
        for (int i = 0; i < 333; i++) for (int j = 0; j < 333; j++) {
            dp[i][j] = -1;
        }
        int ans = max(0, rec(0, n - 1));
        cout << ans << endl;
    }
}