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 <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <list>
#include <iomanip>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
int w[1005];
int dp[1005][1005];
int n;
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-1;mid++)
    {
      res=max(res,rec(l,mid)+rec(mid,r));
    }
  return dp[l][r]=res;
}

int main(void)
{
  while(cin >> n && n)
    {
      memset(dp,-1,sizeof(dp));
      memset(w,0,sizeof(w));
      for(int i=0;i<n;i++)
	{
	  cin >> w[i];
	}
      cout << rec(0,n) << endl;
    }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<deque>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_bacq
#define MP maqe_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[310][310];

int main()
{
	int n;
	
	while(cin >> n,n)
	{
		REP(i,310)REP(j,310)dp[i][j] = 0;
		
		vector<int> v(n);
		REP(i,n)cin >> v[i];
		
		REP(i,n-1)if(abs(v[i]-v[i+1]) < 2)dp[i][i+1] = 2;
		
		for(int q = 2;q < n;q++)
		{
			for(int i = 0;i+q < n;i++)
			{
				int j = i+q;
				for(int k = 0;k < j-i;k++)
				{
					dp[i][j] = max(dp[i][j],dp[i][i+k]+dp[i+k+1][j]);
				}
				if(dp[i+1][j-1] == j-i-1)
				{
					int tmp = 0;
					if(abs(v[i]-v[j])<2)tmp = 2;
					dp[i][j] = max(dp[i][j],j-i-1+tmp);
				}
			}
		}
		
		cout << dp[0][n-1] << endl;
		
	}
	
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, to) for (int i = 0; i < (to); i++)
#define rrep(i, to) for (int i = (to) - 1; i >= 0; i--)
#define repf(i, from, to) for (int i = (from); i < (to); i++)
#define all(v) v.begin(), v.end()
#define unless(cond) if (!(cond))
using namespace std;
using ll = long long;
template <typename T>
using V = vector<T>;
template <typename T, typename U>
using P = pair<T, U>;

int main() {
  while(true) {
    int n;
    cin >> n;

    if(n == 0) {
      break;
    }

    V<int> a(n);

    rep(i, n) {
      cin >> a[i];
    }

    V<V<int>> dp(n, V<int>(n));
    rep(i, n) {
      dp[i][i] = 0;
      int j = i + 1;
    }

    int len = 2;
    for(int i = 0; i + len <= n; i++)  {
      int j = i + len - 1;

      if(abs(a[i] - a[j]) <= 1) {
        dp[i][j] = 2;
      } else {
        dp[i][j] = 0;
      }
    }

    for(len = 3; len <= n; len++) {
      for(int i = 0; i + len <= n; i++) {
        int j = i + len - 1;

        int cand1 = 0;
        if(dp[i + 1][j - 1] == len - 2) {
          cand1 = len - 2;

          if(abs(a[i] - a[j]) <= 1) {
            cand1 += 2;
          }
        }


        int cand2 = 0;
        repf(k, i, j) {
          cand2 = max(cand2, dp[i][k] + dp[k + 1][j]);
        }

        dp[i][j] = max(cand1, cand2);
      }
    }
    cout << dp[0][n - 1] << endl;
  }
}

C++: #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long ll;

int N;
vector<int> w;

int solve(){
	vector<vector<int>> dp(N+1,vector<int>(N+1,-N));
	for(int i = 0; i <= N; i++){
		dp[i][i] = 0;
		if( i < N) dp[i][i+1] = 0;
	}
	for(int bet = 2; bet <= N; bet++){
		for(int i = 0; i + bet <= N; i++){
			int j = i + bet;
			if( dp[i+1][j-1] == j- i -2){
				if( abs(w[i] - w[j-1]) <= 1) dp[i][j] = max(dp[i][j],j-i);
				else dp[i][j] = max(dp[i][j],j-i-2);
			}
			for(int k = i + 1; k <= j-1; k++){
				dp[i][j] = max(dp[i][j],dp[i][k] + dp[k][j]);
			}
		}
	}
	return dp[0][N];
}

int main()
{
	while(cin>>N,N){
		w.resize(N);
		for(int i = 0; i < N; i++) cin>>w[i];
		cout<<solve()<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int N;
int W[300];
int dp[300][300];
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0){break;}
		for(int i=0;i<N;i++){
			scanf("%d",&W[i]);
		}
		for(int i=0;i<300;i++){
			for(int j=0;j<300;j++){
				dp[i][j]=0;
			}
		}
		for(int i=0;i<=N-2;i++){
			if(abs(W[i]-W[i+1])<=1){dp[i][1]=2;}
		}
		for(int j=3;j<=N-1;j+=2){
			for(int i=0;i+j<N;i++){
				//[i,i+j]
				if(abs(W[i+j-1]-W[i+j])<=1){
					dp[i][j]=max(dp[i][j],dp[i][j-2]+2);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i][j-2]);
				}
				if(abs(W[i]-W[i+1])<=1){
					dp[i][j]=max(dp[i][j],dp[i+2][j-2]+2);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i+2][j-2]);
				}
				if(abs(W[i]-W[i+j])<=1&&dp[i+1][j-2]==j-1){
					dp[i][j]=max(dp[i][j],j+1);
				}
				else{
					dp[i][j]=max(dp[i][j],dp[i+1][j-2]);
				}
			}
		}
		if(N%2==0){printf("%d\n",dp[0][N-1]);}
		else{printf("%d\n",max(dp[0][N-2],dp[1][N-2]));}
	}
	return 0;
}