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>
#define ll long long
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
using namespace std;

int main() {
    while(true){
        ll n;
        cin>>n;
        if(n==0)break;
        vector<ll> w(n);
        vector<vector<ll>> dp(n,vector<ll>(n+1,0));
        rep(i,n)cin>>w[i];
        for(ll d=2;d<=n;d++){
            for(ll l=0;l<n;l++){
                ll r=l+d;
                if(r>n)break;
                if(abs(w[l]-w[r-1]) <= 1 && dp[l+1][r-1] == d-2){
                    dp[l][r]=d;
                }
                for(ll m=l+1;m<r;m++){
                    dp[l][r] = max(dp[l][r],dp[l][m]+dp[m][r]);
                }
            }
        }
        cout<<dp[0][n]<<endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E17;

#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
///////////////////////////////////////

bool ans(ll n){
    ll a[n];rep(i,0,n-1)cin>>a[i];
    bool dp[n][n+1];memset(dp,false,sizeof(dp));
    rep(i,0,n-1)dp[i][i]=true;
    for(ll c=2;c<=n;c+=2){
        rep(i,0,n-1){
            ll j=i+c;//[i,j)
            if(j>n)continue;
            if(dp[i+1][j-1]&&abs(a[i]-a[j-1])<=1)dp[i][j]=true;
            for(ll k=i+2;k<=j-2;k+=2){
                if(dp[i][k]&&dp[k][j])dp[i][j]=true;
            }
        }
    }
    /*rep(i,0,n-1){
        rep(j,0,n){
            cout<<dp[i][j]<<" ";
        }cout<<endl;
    }cout<<endl;*/
    ll dp2[n][n];memset(dp2,0,sizeof(dp2));
    rep(i,0,n-1){
        ll ma=0;
        rep(j,i+1,n){
            if(dp[i][j])ma=j-i;
            dp2[i][j-1]=ma;
        }
    }
    /*rep(i,0,n-1){
        rep(j,0,n-1){
            cout<<dp2[i][j]<<" ";
        }cout<<endl;
    }cout<<endl;*/
    ll cou[n][n];
    memset(cou,0,sizeof(cou));
    rep(i,0,n-2){
        rep(j,i,n-1){
            if(i-1>=0)cou[i+1][j]=max(cou[i][j],cou[i][i-1]+dp2[i][j]);
            else cou[i+1][j]=max(cou[i][j],dp2[i][j]);
        }
    }
    /*rep(i,0,n-1){
        rep(j,0,n-1){
            cout<<cou[i][j]<<" ";
        }cout<<endl;
    }cout<<endl;*/
    
    cout<<cou[n-1][n-1]<<endl;
    return 0;
}

int main(){fastio
    while(1){
        ll n;cin>>n;
        if(n==0)return 0;
        ans(n);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

int solve(int N)
{
  vector<long long> daruma(N);
  rep(i, N)
  {
    cin >> daruma[i];
  }
  vector<vector<long long>> dp(N, vector<long long>(N, 0));
  for(int step = 1; step < N; step++)
  {
    rep(l, N)
    {
      int r = l + step;
      if(r > N - 1)
        continue;

      if(dp[l + 1][r - 1] == r - l - 1 && abs(daruma[l] - daruma[r]) < 2)
        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]);
      }
    }
  }
  
  long long dst = 0;
  rep(l, N) rep(r, N)
  {
    dst = max(dst, dp[l][r]);
  }
  return dst;
}

int main()
{
  while(true)
  {
    int N;
    cin >> N;
    if(N != 0)
      cout << solve(N) << endl;
    else
      break;
  }
  return 0;
}

C++:  #include<bits/stdc++.h>
 using namespace std;
#define INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
int N;
bool ok[1001][1001];
int dp[1001][1001];
int ans[1001];
int main(){
    while(cin>>N,N){
       vector<int>v(N);
       REP(i,N)cin>>v[i];
       REP(i,1001)REP(j,1001){ok[i][j]=false;dp[i][j]=0;}
       //ok????????????
       /*REP(i,N){
           for(int j=i+1;j<N;j++){
               if(j-i==1&&abs(v[i]-v[j])<=1){ok[i][j]=true;dp[i][j]=2;continue;}
               if(j-i==2){ok[i][j]=false;dp[i][j]=0;continue;}
               for(int k=i+1;k+1<j;k++){
                   if(abs(v[i]-v[j])>1)continue;
                   if(ok[i][k]==false)continue;
                   if(ok[k+1][j]==false)continue;
                   ok[i][j]=true;
                   dp[i][j]=j-i+1;
               }
           }
       }*/
       for(int k=1;k<=N-1;k++){
          REP(i,N){
              if(i+k>=N)continue;
              if(k==1&&abs(v[i]-v[i+1])<=1){ok[i][i+1]=true;dp[i][i+1]=2;continue;};
              if(k==2){ok[i][i+2]=false;dp[i][i+2]=0;continue;}
              if(abs(v[i]-v[i+k])<=1&&ok[i+1][i+k-1]==true){ok[i][i+k]=true;dp[i][i+k]=k+1;}
              for(int j=1;j<=k;j++){
                  if(ok[i][i+j]&&ok[i+j+1][i+k]){ok[i][i+k]=true;dp[i][i+k]=k+1;}
                  if(abs(v[i]-v[i+j]>1))continue;
                  if(ok[i][i+j-1]==false)continue;
                  if(ok[i+j+1][i+k]==false)continue;
                  ok[i][i+k]=true;
                  dp[i][i+k]=k+1;
              }
          }
       }
       REP(i,1001){
           ans[i]=0;
       }
       for(int i=1;i<N;i++){
           ans[i]=max(ans[i],ans[i-1]);
           ans[i]=max(ans[i],dp[0][i]);
           for(int j=1;j<i;j++){
               ans[i]=max(ans[i],ans[j]+dp[j+1][i]);
           }
       }
       //REP(i,N){REP(j,N)cout<<dp[i][j]<<" ";cout<<endl;}
       //REP(i,N)cout<<ans[i]<<endl;
       cout<<ans[N-1]<<endl;
    }
    return 0;
}