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;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &&x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define INSEG(l,x,r) ((l)<=(x)&&(x)<(r))
#define dump(...)
#define pb push_back
#define _ 0
const signed INF_=1001001001; const long long INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;}
template <class T> void PSUM(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct before_main_function {
    before_main_function() {
        cin.tie(0); ios::sync_with_stdio(false);
        cout << setprecision(15) << fixed;
        // #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

int n;
vector<int> w;
int memo[300][300];

// [l, r]
int rec(int l, int r) {
    auto& MEMO = memo[l][r];
    if (MEMO != INF) return MEMO;
    if (r <= l) return 0;
    
    int ret = 0;
    if (abs(w[l] - w[r]) <= 1 && rec(l + 1, r - 1) == (r - 1) - (l + 1) + 1) {
        chmax(ret, r - l + 1);
    }
    REPF(i, l, r) {
        chmax(ret, rec(l, i) + rec(i + 1, r));
    }
    return MEMO = ret;
}

signed main() {
    while (1) {
        cin >> n;
        if (n == 0) break;
        REP(i, 300) REP(j, 300) memo[i][j] = INF;
        w.resize(n);
        cin >> w;
        cout << rec(0, n - 1) << endl;
    }
    return (0^_^0);
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;

int main() {
    int n;
    while (cin >> n, n) {
        vi a(n);
        for (int i=0; i<n; ++i) cin >> a[i];

        vvi dp(301, vi(301, 0));
        for (int i=0; i+2<=n; ++i) {
            dp[i][i+2] = (abs(a[i] - a[i+1]) <= 1 ? 2 : 0);
        }
        for (int l=3; l<=n; ++l) {
            for (int i=0; i+l<=n; ++i) {
                if (dp[i+1][i+l-1] == l-2) {
                    dp[i][i+l] = dp[i+1][i+l-1] + (abs(a[i] - a[i+l-1]) <= 1 ? 2 : 0);
                }
                for (int j=i+1; j<i+l; ++j) {
                    dp[i][i+l] = max(dp[i][i+l], dp[i][j] + dp[j][i+l]);
                }
            }
        }
        cout << dp[0][n] << endl;
    }
    return 0;
}

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++) {
        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<bits/stdc++.h>
using namespace std;
int n,a[300];
int dp[301][301];

int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++)cin>>a[i];
    memset(dp,0,sizeof(dp));
    for(int k=2;k<=n;k++){
      for(int i=0;i+k<=n;i++){
        dp[i][i+k]=dp[i+1][i+k];
        for(int j=0;j<k-1;j++){
          if(abs(a[i]-a[i+1+j])<=1&&dp[i+1][i+1+j]==(i+1+j)-(i+1)){
            dp[i][i+k]=max(dp[i][i+k],dp[i+1][i+1+j]+dp[i+1+j+1][i+k]+2);
          }
        }
      }
    }
    cout<<dp[0][n]<<endl;
  }
  return 0;
}