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 lint = long long;
using VI = vector<int>;
using VVI = vector<VI>;
int rec(int l, int r, VI &a) {
  if (r - l <= 1) return 0;
  if (r - l == 2) {
    if (abs(a[l + 1] - a[l]) <= 1) return 2;
    return 0;
  }
  int res = rec(l + 1, r, a);
  for (int i = l + 1; i < r; i++) {
    if (abs(a[i] - a[l]) <= 1 and rec(l + 1, i, a) == i - (l + 1)) {
      res = max(res, i + 1 - l + rec(i + 1, r, a));
    }
  }
  return res;
}
int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  while (true) {
    int n; cin >> n;
    if (n == 0) break;
    VI a(n); for (int i = 0; i < n; i++) cin >> a[i];
    cout << rec(0, n, a) << '\n';
  }
  return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

const int INF = 1LL << 29;

int main() {
    int N, w[300];
    while (true) {
        cin >> N;
        if (N == 0) break;
        for (int j = 0; j < N; ++j) cin >> w[j];

        int dp[305][305];
        for (int j = 0; j < N; ++j) {
            fill(dp[j], dp[j]+N+1, 0);
        }

        for (int j = 0; j+1 < N; ++j) {
            if (abs(w[j] - w[j+1]) <= 1) {
                dp[j][j+2] = 2;
            }
        }

        for (int d = 3; d <= N; ++d) {
            for (int j = 0; j + d <= N; ++j) {
                for (int k = 1; j+k < j+d; ++k) {
                    dp[j][j+d] = max(dp[j][j+d], dp[j][j+k] + dp[j+k][j+d]);
                }

                if (dp[j+1][j+d-1] == d-2 && abs(w[j] - w[j+d-1]) <= 1) {
                    dp[j][j+d] = d;
                }
            }
        }

        cout << dp[0][N] << endl;
    }
    return 0;
}

C++: // テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N, w[310];
bool dp[310][310];
int rec[310][310];

signed main() {
    while(cin >> N, N) {
        memset(dp, false, sizeof(dp));
        memset(rec, 0, sizeof(rec));

        rep(i,0,N) cin >> w[i];

        repq(x,2,N) rep(l,0,N-x+1) {
            int r = l+x;
            if(x == 2) {
                if(abs(w[l] - w[l+1]) <= 1) dp[l][r] = true;
            }
            else {
                rep(k,l,r) if(dp[l][k] && dp[k][r]) dp[l][r] = true;
            }

            if(dp[l][r]) {
                int c = 1;
                while(1) {
                    int nl = l - c, nr = r + c;
                    if(nl < 0 || nr > N) break;
                    if(abs(w[nl] - w[nr-1]) > 1) break;
                    dp[nl][nr] = true; c++;
                }
                rec[l][r] = r - l;
            }
        }

        repq(x,2,N) rep(l,0,N-x+1) {
            int r = l+x;
            rep(k,l,r) chmax(rec[l][r], rec[l][k] + rec[k][r]);
        }
        cout << rec[0][N] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int dp[301][301];

int main(){
    int n;
    while(cin >> n, n){
        memset(dp, 0, sizeof(dp));
        vector<int> v(n);
        for(int i = 0; i < n; i++)  cin >> v[i];
        for(int 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(v[i]-v[j]) <= 1)  dp[i][j] = dp[i+1][j-1]+2;
                for(int k = i; k < j; k++){
                    dp[i][j] = max(dp[i][j], dp[i][k]+dp[k+1][j]);
                }
            }
        }
        cout << dp[0][n-1] << endl;
    }
    return 0;
}