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++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#define mp make_pair
#define pb push_back

using LL = long long;
using namespace std;

typedef pair<int, int> pii;

int N;
int memo[333][333];
vector<int>A;
int dp(int L, int R) {
	if (R - L <= 1) {
		return 0;
	}
	if (memo[L][R] >= 0) {
		return memo[L][R];
	}
	int ans = 0;
	for (int i = L + 1; i < R; ++i) {
		ans = max(ans, dp(L, i) + dp(i, R));
	}
	for (int i = L; i < R - 1; ++i) {
		if (abs(A[i] - A[i + 1]) <= 1) {
			ans = max(ans, dp(L, i) + dp(i + 2, R) + 2);
		}
	}
	if (dp(L + 1, R - 1) == R - L - 2) {
		if (abs(A[L] - A[R - 1]) <= 1) {
			ans = max(ans, R - L);
		}
	}
	return memo[L][R] = ans;
}

int main() {
	while (cin >> N, N) {
		memset(memo, 0xff, sizeof(memo));
		A.clear();
		for (int i = 0; i < N; ++i) {
			int tmp;
			cin >> tmp;
			A.push_back(tmp);
		}
		cout << dp(0, N) << endl;
	}
	return 0;
}

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

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

int main(){
  int n;
	bool dp[303][303];
	int ans[303];
  while(cin>>n,n){
    vector<int> w(n);
    REP(i,n)cin>>w[i];
    fill(dp[0],dp[n],false);
    fill(ans,ans+n,0);
    for(int i = 0;i <n;i++)dp[i][i+1] |= abs(w[i+1]-w[i])<=1;
    for(int width = 2;width <= n;width++){
      for(int l = 0;l + width <= n;l++){
				int r = l + width-1;
				for(int d = l;d < r;d++){//-1??¨???
					dp[l][r] |= dp[l][d]&&dp[d+1][r];
					dp[l][r] |= !(width&1) && dp[l+1][r-1] && abs(w[l]-w[r]) <=1;
				}
      }
    }
		/*		FOR(width,1,n+1){
			REP(l,n-width){
				cout << dp[l][l+width] << ' ';
				}
			cout << endl;
			}*/
    for(int i = 0;i<n;i++){
			//i-1?????§????????£????????????
			//i??\????????´??°??§??????????????????i???????????¶??§i-1??????????????????
			//ans[i]???????????´??°???????????§????????????divide??°??????????????????????????¢?????´??°???????????????
			if(i > 0)ans[i] = max(ans[i],ans[i-1]);
      for(int r = i+1;r<n;r++){
				//i-1?????§????????£??????????????????i???????§???????????¶????????????????????????¨?????´??°????????????
				if(dp[i][r]) ans[r] = max(ans[r],(i>0?ans[i-1]:0) + (r-i+1));
      }
    }
    cout << ans[n-1] << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
#define PIE 3.14159265358979
#define N_MAX 305
#define rep(i,n) for(int i = 0;i < (n);i++)
using namespace std;

int n;
int a[N_MAX];
int dp[N_MAX][N_MAX];

int main(){
    while(cin>>n,n!=0){
        rep(i,n){
            cin>>a[i];
        }
        rep(i,n+1){
            rep(j,n+1){
                dp[i][j]=0;
            }
        }
        if(n<2){
            cout<<0<<endl;
            continue;
        }
        int num = 2;
        while(num<=n){
            int left = 0;
            while(left+num<=n){
                if(dp[left+1][left+num-1]==num-2&&abs(a[left]-a[left+num-1])<=1)
                    dp[left][left+num]=num;
                int div=1;
                while(div<num){
                    dp[left][left+num]=max(dp[left][left+num],dp[left][left+div]+dp[left+div][left+num]);
                    div++;
                }
                left++;
            }
            num++;
        }
        int ans = 0;
        rep(i,n+1){
            rep(j,n+1){
                ans=max(ans,dp[i][j]);
            }
        }
        cout<<ans<<endl;
    }
    
}

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

using namespace std;

using ll = long long;

#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define REP(i, n) for(int i=0;i<(n);++i)
#define ALL(v) (v).begin(),(v).end()

typedef pair<int, int> P;

template<class T>
bool chmax(T &a, const T &b) {
	if (a < b) {
		a = b;
		return 1;
	}
	return 0;
}

template<class T>
bool chmin(T &a, const T &b) {
	if (b < a) {
		a = b;
		return 1;
	}
	return 0;
}

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

int main(void) {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	while (1) {
		cin >> n;
		if (n == 0) break;
		REP(i, n) cin >> w[i];
		memset(dp, 0, sizeof(dp));
		
		for (int i = n - 1; i >= 0; --i) {
			dp[i][i] = 0;
			dp[i][i + 1] = 0;
			for (int j = i + 1; j <= n; ++j) {
				if (i + 1 < j && abs(w[i] - w[j - 1]) <= 1 && dp[i + 1][j - 1] == j - i - 2) {
					dp[i][j] = j - i;
				}
				for (int k = j + 1; k <= n; ++k) {
					chmax(dp[i][k], dp[i][j] + dp[j][k]);
				}
			}
		}
		
		cout << dp[0][n] << endl;
	}
	
	return 0;
}

C++: //ICPC2016 domestic D ??????????????¨???
//dp[l][r] := //??????[l, r)??§????????§?????¨???????????¢????????°???????????????
//?????¬???????????????????????????????????°??§????????§?????????w[l]??¨w[r - 1]??????????°??????????????????¨?¶??????§????????¨???????????????????£??????\????????????
//????£?30?????????????????°30????????????
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);++i)
#define rep(i,n) REP(i, 0, n)
int dp[301][301];
int w[301];
int calc(int l, int r) {//??????[l, r)??§????????§???
    int& res = dp[l][r];
    if(dp[l][r] != -1) return res;
    res = 0;
    if(r - l <= 1) return res;
    if(r - l == 2)
      return abs(w[l] - w[l + 1]) <= 1 ? res = 2 : res;
    //?????????
    REP(mid, l + 1, r - 1)
      res = max(res, calc(l, mid) + calc(mid, r));
    //??????????????¨?¶??????§??????????????§??????
    int temp = calc(l + 1, r - 1);
    if(temp == r - l - 2 && abs(w[l] - w[r - 1]) <= 1) temp += 2;
    res = max(res, temp);
    return res;
}
int main() {
    int N;
    while(~scanf("%d", &N) && N) {
        memset(dp, -1, sizeof(dp));
        memset(w, 0, sizeof(w));
        rep(i, N) scanf("%d", w + i);
        printf("%d\n", calc(0, N));
    }
    return 0;
}