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;

    int dp[301] = {0};

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

    ans = dp[n - 1];

    /*
    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>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

bool check(int a, int b){
	return abs(a - b) <= 1;
}

int main(){
    int n;
    while(cin >> n,n){
        vector<int> w(n);
        rep(i,n){
            cin >> w[i];
        }

		int dp[300][300] = {{0}};
		range(W,2,n + 1){
			rep(i,n){
				int j = i + W - 1;
				if(i + W - 1 >= 300) break;
				if(check(w[i], w[j]) && dp[i + 1][j - 1] == W - 2){
					dp[i][j] = W;
				}
				range(k,i,j){
					dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
				}
			}
		}
		cout << dp[0][n - 1] << endl;
    }
}

C++: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int dp[301][301];

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int> ans;
    while(true){
        int N; cin >> N; if(N == 0) break;
        vector<int> W(N); rep(i, N) cin >> W[i];
        memset(dp, 0, sizeof(dp));
        for(int haba = 1; haba <= N; haba++){
            for(int left = 0; left + haba <= N; left++){
                int res = 0;
                int right = left + haba;
                if(right-left >= 2){
                    if(abs(W[left]-W[right-1]) <= 1 && dp[left+1][right-1] == right-left-2) res = dp[left+1][right-1]+2;
                    else res = dp[left+1][right-1];
                }
                for(int boarder = left+1; boarder < right; boarder++){
                    res = max(res, dp[left][boarder] + dp[boarder][right]);
                }
                dp[left][right] = res;
            }
        }
        ans.push_back(dp[0][N]);
    }
    rep(i, ans.size()) cout << ans[i] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin>>n && n!=0){
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        bool ok[n][n]={};
        map<int,vector<int>> mp;
        for(int i=0;i<n;i++) ok[i][i]=true;
        for(int i=0;i<n-1;i++){
            if(abs(a[i]-a[i+1])<=1) ok[i][i+1]=true;
        }

        for(int len=1;len<n;len+=2){
            for(int i=0;i<n-len;i++){
                for(int k=1;k<i+len;k++){
                    if(ok[i][i+k] && ok[i+k+1][i+len]) ok[i][i+len]=true;
                }
                if(abs(a[i]-a[i+len])<=1 && ok[i+1][i+len-1]) ok[i][i+len]=true;
                
                if(ok[i][i+len]) mp[i+len].push_back(len+1);
            }
        }
        
        int dp[n]={};
        for(int i=1;i<n;i++){
            dp[i]=dp[i-1];
            for(auto j:mp[i]){
                if(i-j>=0) dp[i]=max(dp[i],dp[i-j]+j);
                if(i-j==-1) dp[i]=max(dp[i],j);
            }
        }
        cout<<dp[n-1]<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int DP[301][301];

int main()
{
	while (1) {
		int n; cin >> n;
		if (n == 0)break;
		vector<int>A(n);
		for (int i = 0; i < n; i++)cin >> A[i];

		rep(i, 301)rep(j, 301)DP[i][j] = 0;

		for (int i = n-2; i >= 0; i--) {
			for (int j = i + 1; j <= n - 1; j++) {
				for (int k = j + 1; k <= n; k++) {
					if (abs(A[i]-A[k-1])<=1 && DP[i + 1][k - 1] == k - i - 2)DP[i][k] = k - i;
					DP[i][k] = max(DP[i][k], DP[i][j] + DP[j][k]);
				}
			}
		}
		cout << DP[0][n] << endl;
	}

    return 0;
}