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 <limits.h>
#include <algorithm>
#include <vector>
#include <map>
#include <math.h>
using namespace std;

int n, w[310];
int dp[310][310];

int maxx(int i, int j) {
    if (i > j) return i;
    else return j;
}

bool solve(int i, int j) {
    if (dp[i][j] >= 0) return dp[i][j];
    else {
        if (i >= j || (j - i) % 2 == 0 || i >= n || j >= n) {
            dp[i][j] = 0;
            return dp[i][j];
        }
        if (j - i == 1) {
            if (abs(w[i] - w[j]) <= 1) dp[i][j] = 1;
            else dp[i][j] = 0;
            return dp[i][j];
        }
        
        if (solve(i + 1, j - 1) == 1) {
            if (abs(w[i] - w[j]) <= 1) dp[i][j] = 1;
            else dp[i][j] = 0;
        }
        
        for (int k = 2; j - k > 0; k += 2) {
            for (int l = 2; i + l < j; l += 2) {
                if (solve(i, j - k) == 1 && solve(i + l, j) == 1 && k + l == j - i + 1) {
                    dp[i][j] = 1;
                    return dp[i][j];
                }
            }
        }
        
        
        if (dp[i][j] == -1) dp[i][j] = 0;
        
        return dp[i][j];
    }
}
    
int main(void){
    while (1) {
        cin >> n;
        if (n == 0) return 0;
        for (int i = 0; i < n; i++) cin >> w[i];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) dp[i][j] = -1;
        }
        
        
        int min = 0, max = 0, ans = 0;
        int min2 = 0, max2 = 0, ans2 = 0;
        int min3 = 0, max3 = 0, ans3 = 0;
        for (int i = 0; i < n; i++) {
            bool mmm = true, mmm2 = true, mmm3 = true;
            if (min > i) mmm = false;
            if (min2 > i) mmm2 = false;
            if (min3 > i) mmm3 = false;
            for (int j = n - 1; j >= 0; j--) {
                if (!mmm && !mmm2 && mmm3 && solve(i, j) == 1 && solve(i, j + 2) != 1 && min3 <= i) {
                        max3 = j;
                        mmm3 = false;
                    //cout << "m3 " << max2 << endl;
                }
                if (!mmm && mmm2 && solve(i, j) == 1 && solve(i, j + 2) != 1 && min2 <= i) {
                        max2 = j;
                        mmm2 = false;
                    //cout << "m2 " << max2 << endl;
                }
                if (mmm && solve(i, j) == 1 && min <= i) {
                    max = j;
                    mmm = false;
                }
            }
            /*
            if (!mmm2 && mmm3) {
                max3 = max2;
                mmm3 = false;
            }
            */
            if (max > min) {
                ans += (max - i) + 1;
                min = max + 1;
                max = min;
            }
            else {
                min = maxx(min, i);
                max = min;
            }
            
            if (max2 > min2) {
                ans2 += (max2 - i) + 1;
                min2 = max2 + 1;
                max2 = min2;
            }
            else {
                min2 = maxx(min2, i);
                max2 = min2;
            }
            
            if (max3 > min3) {
                ans3 += (max3 - i) + 1;
                min3 = max3 + 1;
                max3 = min3;
            }
            else {
                min3 = maxx(min3, i);
                max3 = min3;
            }
            
            if (max == max2 && max2 == max3) {
                ans = maxx(ans, ans2);
                ans = maxx(ans, ans3);
                ans2 = ans;
                ans3 = ans;
                //cout << "equal" << endl;
            }
            
            else if (max2 == max3 + 1) {
                ans2 = maxx(ans2, ans3);
                ans3 = ans2;
                //cout << "equalf" << endl;
            }
            
            
            //cout << "min " << min << " max " << max 
           //    << " min2 " << min2 << " max2 " << max2 
            //    <<  " min3 " << min3 << " max3 " << max3 << endl; 
            
        }
        
        
        
        cout << ans << endl;
        
        /*
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << solve(i, j) << " ";
            }
            cout << endl;
        }
        */
        
    }
    
    
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for(int i = int(a); i < int(b); ++i)

constexpr int MAXN = 302;

bool dp[MAXN][MAXN]; // [l, r)
int dp2[MAXN];

signed main() {

	while (true) {
		int n;
		std::cin >> n;
		if (n == 0) break;
		std::vector<int> w(n);
		REP(i, n) std::cin >> w[i];

		REP(i, MAXN) REP(j, MAXN) dp[i][j] = false;
		REP(i, MAXN) dp2[i] = 0;

		REP(i, n - 1) {
			if (std::abs(w[i] - w[i + 1]) <= 1) dp[i][i + 2] = true;
		}
		FOR(d, 4, n + 1) {
			REP(l, n - 1) {
				int r = l + d;
				if (r > n) break;
				FOR(m, l + 2, r - 1) {
					if (dp[l][m] && dp[m][r]) dp[l][r] = true;
				}
				if (std::abs(w[l] - w[r - 1]) <= 1 && dp[l + 1][r - 1]) dp[l][r] = true;
			}
		}

		/*REP(l, n + 1) {
			REP(r, n + 1) {
				std::cout << dp[l][r] << " ";
			}
			std::cout << std::endl;
		}*/

		REP(l, n) {
			dp2[l + 1] = std::max(dp2[l + 1], dp2[l]);
			FOR(r, l, n + 1) {
				if (!dp[l][r]) continue;
				dp2[r] = std::max(dp2[r], dp2[l] + (r - l));
			}
		}

		std::cout << dp2[n] << "\n";
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

bool check(int a, int b){
	return abs(a - b) <= 1;
}

int main(){
    int n;
    while(cin >> n,n){
        vector<int> w(n);
        rep(i,n){
            cin >> w[i];
        }

		int dp[300][300] = {{0}};
		range(W,2,n + 1){
			rep(i,n){
				int j = i + W - 1;
				if(check(w[i], w[j]) && dp[i + 1][j - 1] == W - 2){
					dp[i][j] = W;
				}
				range(k,i,j){
					dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
    }
}