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;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=305,INF=1<<25;
int A[MAX],dp[MAX][MAX];

int solve(int l,int r){
    if(dp[l][r]!=-1) return dp[l][r];
    if(r-l<=1) return dp[l][r]=0;
    if(r-l==2){
        if(abs(A[l]-A[l+1])<=1) return dp[l][r]=2;
        else return dp[l][r]=0;
    }
    
    int ma=0;
    
    for(int i=l+1;i<=r-1;i++){
        ma=max(ma,solve(l,i)+solve(i,r));
    }
    
    if(solve(l+1,r-1)==r-l-2) ma=max(ma,solve(l+1,r-1)+2*(abs(A[l]-A[r-1])<=1));
    
    return dp[l][r]=ma;
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        for(int i=0;i<N;i++) cin>>A[i];
        memset(dp,-1,sizeof(dp));
        
        cout<<solve(0,N)<<endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#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(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll n;
ll w[301];
ll dp[301][301];
bool f[301][301];

int main() {
	while (cin >> n&&n) {
		rep(i, 301)rep(j, 301) {
			dp[i][j] = 0;
			f[i][j] = 0;
		}
		rep(i, n)cin >> w[i];
		for (int wid = 1; wid < n; wid += 2) {
			rep(i, n) {
				if (i + wid >= n)continue;
				if (wid == 1) {
					if (abs(w[i] - w[i + wid]) <= 1) {
						dp[i][i + wid] = 2;
						f[i][i + wid] = 1;
					}
				}
				else {
					if (abs(w[i] - w[i + wid]) <= 1) {
						if (f[i + 1][i + wid - 1]) {
							dp[i][i + wid] = dp[i + 1][i + wid - 1] + 2;
							f[i][i + wid] = 1;
						}
					}
					for (int j = i + 1; j < i + wid; j += 2) {
						dp[i][i + wid] = max(dp[i][i + wid], (dp[i][j] + dp[j + 1][i + wid]));
						f[i][i + wid] |= (f[i][j] && f[j + 1][i + wid]);
					}
				}
			}
		}
		ll ans = 0;
		rep(i, n) {
			//ll sum = 0;
			FOR(j, i + 1, n) {
				//cout << i << " " << j << " " << dp[i][j] << endl;
				ans = max(ans, dp[i][j]);
				//if (dp[i][j])sum += max(ans, (ll)(j - i + 1));
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int dp[302][302];
int main() {
    int n;
    while(cin >> n, n){
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= n; ++j) {
                dp[i][j] = -INF<int>;
            }
            dp[i][i] = 0;
        }
        vector<int> v(n);
        for (auto &&i : v) scanf("%d", &i);
        for (int len = 2; len <= n; len += 2) {
            for (int i = 0; i+len <= n; ++i) {
                int j = i+len;
                if(abs(v[j-1]-v[i]) <= 1){
                    dp[i][j] = max(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][j]);
                }
            }
        }
        for (int i = 0; i <= n; ++i) {
            dp[i][i+1] = 0;
        }
        for (int len = 1; len <= n; ++len) {
            for (int i = 0; i+len <= n; ++i) {
                int j = i+len;
                for (int k = i; k <= j; ++k) {
                    dp[i][j] = max(dp[i][j], dp[i][k]+dp[k][j]);
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= n; ++j) {
                ans = max(ans, dp[i][j]);
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

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

int main() {
  unsigned int N = 0;
  vector<int> num;
  int w = 0;

  cin >> N;
  while(N != 0) {
    for(unsigned int i = 0; i < N; i++) {
      cin >> w;
      num.push_back(w);
    }

    while(true) {
      unsigned int i;
      for(i = num.size()-1; i > 0; i--)
	if(num.at(i)-num.at(i-1) >= -1 && num.at(i)-num.at(i-1) <= 1)
	  break;

      if(i == 0)
	break;
      num.erase(num.begin()+i);
      num.erase(num.begin()+i-1);
      
      if(num.empty())
	break;
    }

    cout << N-num.size() << endl;

    num.clear();

    cin >> N;
  }


  return 0;
}