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;

int SolveDaruma(const int n, const vector<int> &w) {
    vector<vector<int>> s(n, vector<int>(n));

    for (int l = 0; l + 1 < n; ++l)
        if (abs(w[l] - w[l + 1]) <= 1)
            s[l][l + 1] = 2;

    for (int i = 3; i <= n; ++i)
        for (int l = 0; l + i <= n; ++l) {
            const int r = l + i - 1;

            if (abs(w[l] - w[r]) <= 1 && s[l + 1][r - 1] == i - 2) {
                s[l][r] = i;
                continue;
            }

            for (int m = l + 1; m <= r; ++m)
                s[l][r] = max(s[l][r], s[l][m - 1] + s[m][r]);
        }

    return s[0][n - 1];
}

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

    int n;

    while (cin >> n, n) {
        vector<int> w(n);
        for (auto &x : w) cin >> x;

        cout << SolveDaruma(n, w) << '\n';
    }

    return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1611&lang=jp
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int n;
    while(cin >> n, n)
    {
        int dp[305][305] = {};
        vector<int> w(n);

        for(auto& _w : w)
        {
            cin >> _w;
        }

        for(int b = 2; b <= n; b++)
        for(int l = 0; l < n; l++)
        {
            int r = l + b - 1;
                if( r >= n )break;

                if( abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == r - l - 1)
                {
                    dp[l][r] = r - l + 1;
                }
                else
                for(int k = l; k < r; k++)                
                {
                    dp[l][r] = max( dp[l][r], dp[l][k] + dp[k + 1][r] );
                }
        }
        cout << dp[0][n - 1] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

constexpr int inf = 1e9;
vector<int> w(300);
int dp[1000][1000];

int rec(int l,int r){
    //既に計算済み？
    if(dp[l][r] != -1) return dp[l][r];

    //これ以上取り除けない？
    if(abs(l - r) <= 1) return 0;

    int res = 0;
    //パターン1.
    if(abs(w[l] - w[r - 1]) <= 1 && rec(l + 1,r - 1) == r - l - 2)
    {
        //[l , r)がはじき出せるので
        res = r - l;
    }

    //パターン２.区間を分ける
    for(int mid = l + 1;mid <= r - 1;mid++)
    {
        res = max(res , rec(l,mid) + rec(mid,r));
    }
    return dp[l][r] = res;
};

int main(){
    int num;

    while(cin >> num, num != 0){
        memset(dp, -1,sizeof(dp));
        for(int i = 0; i < num; i++){
            cin >> w[i];
        }
        cout << rec(0, num) << endl;
    }

    return 0;
}

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

class Solve {
private:
	using vi = std::vector<int>;
	using vvi = std::vector<vi>;

public:
	bool is_last_query{};
	Solve()
	{
		int n;
		scanf("%d", &n);
		if (n == 0)
		{
			is_last_query = true;
			return;
		}
		vi w(n);
		for (auto& e: w) scanf("%d", &e);

		vvi dp(n, vi(n + 1));
		// [left, right)
		for (int width{2}; width <= n; width++)
			for (int left{}; left + width <= n; left++)
			{
				int right{left + width};
				for (int mid{left + 1}; mid < right; mid++)
					dp[left][right] = std::max(dp[left][right], dp[left][mid] + dp[mid][right]);
				if (std::abs(w[left] - w[right - 1]) <= 1 && dp[left + 1][right - 1] + 2 == width)
					dp[left][right] = width;
			}
		printf("%d\n", dp[0][n]);
	}
};

int main()
{
	while (!Solve().is_last_query);

	return 0;
}

C++: #include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<tuple>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(ll i = 1;i <=  n; i++)
#define rrep(i,n) for(ll i = n -1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
const ll INF = 1e18;
const double eps = 1e-8;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    std::fill((T*)array, (T*)(array + N), val);
}int n, m, s, t, r, Q, b, c; vector<int> w;
int dp[310][310];
int rec(int l, int r) {
    if (dp[l][r] != -1) return dp[l][r];
    if (abs(l - r) <= 1) return 0;
    int res = 0;
    if (abs(w[l] - w[r - 1]) <= 1 && rec(l + 1, r - 1) == r - l - 2)
        res = r - l;
    for (int mid = l + 1; mid < r; mid++)
        res = max(res, rec(l, mid) + rec(mid, r));
    return dp[l][r] = res;
}
int main(void) {
    while (cin >> n, n) {
        w.resize(n); Fill(dp, -1);
        rep(i, n) cin >> w[i];
        cout << rec(0, n) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define N 301
using namespace std;
int w[N],mem[N][N],used[N][N];
 
int dfs(int l,int r){
  if(l==r) return 0;
  if(r-l==1) return 2*(abs(w[l]-w[r])<=1);
  if(used[l][r]++) return mem[l][r];
   
  int res=0;
  if(abs(w[l]-w[r])<=1&&dfs(l+1,r-1)==r-l-1)res=2+dfs(l+1,r-1);
   
  for(int i=l;i<r;i++) res=max(res,dfs(l,i)+dfs(i+1,r));
  return mem[l][r]=res;
}
 
int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>w[i];
    memset(used,0,sizeof(used));
    cout<<dfs(0,n-1)<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n, a[1000], dp[1000][1000];
int main() {
	while (true) {
		cin >> n; if (n == 0)break; for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++)dp[i][j] = 0; }
		for (int i = 0; i < n; i++)cin >> a[i];
		for (int i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) <= 1) { dp[i][i + 1] = 2; } }
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= n; j++) {
				int a1 = j, a3 = i + j; if (a3 >= n)continue;
				for (int k = a1 + 1; k <= a3; k++) {
					int a2 = k;
					dp[a1][a3] = max(dp[a1][a3], dp[a1][a2 - 1] + dp[a2][a3]);
				}
				if (dp[a1][a3] >= 1 && a1 > 0 && a3 < n - 1 && abs(a[a1 - 1] - a[a3 + 1]) <= 1) {
					dp[a1 - 1][a3 + 1] = dp[a1][a3] + 2;
				}
			}
		}
		cout << dp[0][n - 1] << endl;
	}
	return 0;
}