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 <iostream>
#include <list>
#include <vector>
#include <cstdlib>
#include <iterator>

//?????????
struct daruma {
	std::list<int> block;
	int size;
};

class DarumaOtosi {
public:
	DarumaOtosi(daruma d) {
		darumalist.resize(d.size-3);
		darumalist[d.size - 4].push_back(d);
	}
	//???????????????????????????
	int Hunmer(daruma d) {
		//?????????????????????
		if (d.size != d.block.size()) {
			//??????????????????????????´??°
			d.size = d.block.size();
			//????????\????????????????????????????????????
			if (d.size == 0 || d.size == 1) return d.size;
			//?????????????????????????????¨?????£?????£???
			if (d.size >= 4) {
				int tmp = CheckDaruma(d);
				if (tmp != 0) return tmp;
				darumalist[d.size - 4].push_back(d);
			}
		}

		for (auto itr = d.block.begin(); itr != std::prev(d.block.end()); itr++) {
			if (abs(*itr - *(std::next(itr))) <= 1) {
				const int tmp1 = *itr; const int tmp2 = *(std::next(itr));
				//????????????
				itr = d.block.erase(itr, std::next(itr, 2));
				//???????????£??????
				int size = Hunmer(d);
				//????????????
				itr = d.block.insert(itr, tmp2);
				itr = d.block.insert(itr, tmp1);
				//???????????´??°
				if (d.size > size) {
					d.size = size;
					//????????\???????????????????????£?????????
					if (d.size == 0 || d.size == 1) break;
				}
			}
		}

		if(d.block.size() >= 4) darumalist[d.block.size() - 4].back().size = d.size;
		return d.size;
	}

private:
	std::vector<std::vector<daruma>> darumalist;

	//????????????????????????
	void CollectDaruma(daruma d) {
		darumalist[d.size].push_back(d);
	}

	//???????????¨??????????????????????????§??????
	int CheckDaruma(daruma d) {
		int n = d.size;
		for (int i = 0; i < darumalist[n-4].size(); i++) {
			if (d.block == darumalist[n-4][i].block) return darumalist[n-4][i].size;
		}
		return 0;
	}
};

int main() {
	int n = 1;
	std::vector<daruma> daruma_original;

	//??\???
	for (int m;;) {
		daruma daruma;
		std::cin >> n;
		if (n == 0) break;
		for (int i = 0; i < n; i++) {
			std::cin >> m;
			daruma.block.push_back(m);
		}
		daruma.size = daruma.block.size();
		daruma_original.push_back(daruma);
	}

	//???????????????
	for (auto x : daruma_original) {
		DarumaOtosi darumaotosi(x);
		std::cout << x.size - darumaotosi.Hunmer(x) << std::endl;
	}

	return 0;
}

C++: #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 )continue;


                if( abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == r - l - 1)
                {
                    dp[l][r] = r - l + 1;
                }

                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;

using usize = ::std::size_t;
//using u64 = ::std::uint_least64_t;
typedef long long u64;
//static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

u64 N;
u64 dp[4010][4010], A[4010];

u64 dfs(int l , int r){
    if (~dp[l][r]) return dp[l][r];
    if (r == l + 1) {
        if (A[l] - A[r] > 1 || A[r] - A[l] > 1) {
            return dp[l][r] = 0LL;
        }
        return dp[l][r] = 2LL;
    } else if (r == l) {
        return dp[l][r] = 0LL;
    }
    bool valid = false;
    if (l + 1 < r - 1) {
        int i = l + 1, j = r - 1;
        dp[i][j] = dfs(i, j);
        if (j - i + 1 > 2) {
            for (int k = i; k < j; k++) {
                if (dp[i][j] <= dfs(i, k) + dfs(k + 1, j)) {
                    dp[i][j] = dp[i][k] + dp[k + 1][j];
                }
            }
        }
        
        if (dp[i][j] == r - l - 1 && (A[l] - A[r] <= 1 && A[r] - A[l] <= 1)) {
            valid = true;
            if (dp[l][r] <= dp[i][j] + 2) {
                dp[l][r] = dp[i][j] + 2;
            }
        }
        
    }
    
    if (!valid) {
        for (int k = l; k < r; k++) {
            if (dp[l][r] <= dfs(l, k) + dfs(k + 1, r)) {
                dp[l][r] = dp[l][k] + dp[k + 1][r];
            }
        }
    }
    
        
    
    
    return dp[l][r];
    
}

int main(int argc, char *argv[])
{
   
 
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while (cin >> N, N != 0) {
        for (int i = 0; i < N; i++) {
            cin >> A[i];
        }
        
        
        memset(dp, -1, sizeof(dp));
        dfs(0, N - 1);
        
        cout << dp[0][N - 1] << endl;
        
    }
    
    
    return 0;
}