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;

int n;
vector<int> w;

int memo[301][301];

int rec(int l, int r) {
  if (r == l) {
    return (memo[l][r] = 1);
  }
  if (memo[l][r] != 0) {
    return memo[l][r];
  }
  if (r - l == 1) {
    if (abs(w[l] - w[r]) <= 1) {
      return (memo[l][r] = 2);
    } else {
      return (memo[l][r] = 1);
    }
  }

  if (abs(w[l] - w[r]) <= 1) {
    if (rec(l + 1, r - 1) == 2) {
      return (memo[l][r] = 2);
    }
  }

  if (abs(w[l] - w[l + 1]) <= 1) {
    if (rec(l + 2, r) == 2) {
      return (memo[l][r] = 2);
    }
  }

  if (abs(w[r] - w[r - 1]) <= 1) {
    if (rec(l, r - 2) == 2) {
      return (memo[l][r] = 2);    
    }
  }

  return (memo[l][r] = 1);
}


int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    memset(memo, 0, sizeof(memo));

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if ((j - i) % 2 == 0) {
          continue;
        }
        rec(i, j);        
      }
    }

    
    int ans = 0;


    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (memo[i][j] == 2) {
          ans = max(ans, j - i + 1);
        }
        for (int k = i + 1; k <= j; k++) {
          for (int l = k + 1; l < j; l++) {
            if (memo[i][k] == 2 && memo[l][j] == 2) {
              ans = max(ans, (k - i + 1) + (j - l + 1));
            }
          }
        }
      }
    }

    cout << ans << endl;
  }


  return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<numeric>
#include<map>
using namespace std;

#define R(i,s,n) for(int i=s;i<n;i++)
#define D(i,n,s) for(int i=n-1;i>=s;i--)
typedef pair<int,int> P;

const int MAX_N = 3e+2;
const int MAX_W = 1e+3;

int N,W[MAX_N];
int dp[MAX_N][MAX_N];

void solve();

int main() {

  while(cin>>N && N) {
    R(i,0,N) cin>>W[i];
    solve();
  }

  return 0;
}

void solve() {
  memset(dp,0,sizeof(dp));

  //幅ループ
  R(i,1,N){

    //縦軸
    for(int j=0; j+i<N; j++){
      int mx=0;

      //切るとこ
      for(int k=0; k<i; k++){
        //j~j+k,j+k+1~j+i
        mx=max(mx,dp[j][j+k]+dp[j+k+1][j+i]);
      }
      if(i-1 == dp[j+1][j+i-1] && abs(W[j]-W[j+i]) <= 1){
          mx = i+1;
      }
      dp[j][j+i]=mx;
    }
  }

  cout << dp[0][N-1] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(int I = int(A); I < int(B); ++I)
#define FORR(I,A,B) for(int I = int((B)-1); I >= int(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;




int main(){
	int N;
	while(1){
		cin >> N;
		if(N==0)break;
		vector<int> w(N+1);
		FOR(i,1,N+1)cin >> w[i];
		bool dp[N+1][N+1]={};
		int ans = 0;
		for(int i=2;i<N+1;i+=2){//i:幅
			FOR(j,1,N+1){//j:開始位置
				// [j,j+i-1]
				if(j+i-1>N)continue;
				if( (j+1 > j+i-2) && abs(w[j]-w[j+i-1])<=1){
					dp[j][j+i-1]=true;
					ans = max(ans,i);
				}else if(dp[j+1][j+i-2] && abs(w[j]-w[j+i-1])<=1){
					dp[j][j+i-1]=true;
					ans = max(ans,i);
				}
				for(int k=2;k<=i-2;k+=2){
					if(dp[j][j+k-1]&&dp[j+k][j+i-1]){
						dp[j][j+i-1]=true;
						ans = max(ans,i);
					}
				}
			}
		}
		int dpa[N+1][N+1]={};
		FOR(i,1,N)if(dp[i][i+1])dpa[i][i+1]=2;
		FOR(i,3,N+1){//幅
			FOR(j,1,N+1){//開始
				if(j+i-1>N)continue;
				for(int k=0;k<i;k++){
					dpa[j][j+i-1] = max(dpa[j][j+k-1]+dpa[j+k][j+i-1],dpa[j][j+i-1]);
					if(dp[j][j+i-1])dpa[j][j+i-1] = i;
				}
			}
		}
		cout << dpa[1][N] << endl;
	}
}

C++: 	#include <iostream>
	#include <cstdio>
	#include <algorithm>
	#include <string>
	#include <cstring>
	#include <cctype>
	#include <cmath>
	#include <stack>
	#include <queue>
	#include <vector>
	#include <set>
	#include <map>
	#include <list>
	#include <stdio.h>
	#include <string.h>
	#include <cstdlib>
	#include <math.h>
	#include <bitset>
	#include <iterator>
	#include <iomanip>
	#include <sstream>
	#include <numeric>
	#include <cassert>
	using namespace std;
	int dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0};
	long long gcd(long long a,long long b){if(a%b==0){return b;}else return gcd(b,a%b);}
	long long lcm(long long a,long long b){if(a==0){return b;} return a/gcd(a,b)*b;}
	#define INF 2000000000
	#define MOD 1000000007
	#define sym cout<<"---------"<<endl;
	#define ll long long
	#define mk make_pair
	#define en endl
	#define RE return 0
	#define int ll
	#define P pair<int,int>
	
	int dp[305][305];

signed main(){
	int n;
	while(cin>>n&&n){
		int w[305];
		memset(dp, 0, sizeof(dp));
		for(int i=0; i<n; i++) cin>>w[i];
		
		for(int W=2; W<=n; W++){
			for(int l=0; l<n; l++){
				int r=l+W;
				if(r>n) continue;
				
				if(dp[l+1][r-1]==W-2&&abs(w[l]-w[r-1])<=1) dp[l][r]=W;
				
				for(int mid=l; mid<=r; mid++){
					dp[l][r]=max(dp[l][r], dp[l][mid]+dp[mid][r]);
				}
			}
		}
		cout<<dp[0][n]<<en;
	}
}