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 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 <string>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#include <fstream>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;

vector<string> g;

vector<vector<ll>> dp;

ll search(ll i, ll j, const vector<ll>& w)
{
    if (i > w.size() - 1 || j > w.size() - 1)
    {
        return 0;
    }
    if (dp[i][j] > -1)
    {
        return dp[i][j];
    }
    if (i >= j)
    {
        return 0;
    }
    dp[i][j] = 0;
    if (j - i == 1)
    {
        if (abs(w[i] - w[j]) <= 1)
        {
            dp[i][j] = 1;
            return 1;
        }
    }
    ll ret = max(search(i + 1, j, w), search(i, j - 1, w));
    if (abs(w[i] - w[j]) <= 1 && (j - i) % 2)
    {
        ll cur = search(i + 1, j - 1, w);
        if (2 * cur == j - i - 1)
        {
            ret = max(ret, cur + 1);
        }
    }
    for (ll k = i + 1; k < j; ++k)
    {
        ret = max(ret, search(i, k, w) + search(k + 1, j, w));
    }
    dp[i][j] = ret;
    return ret;
}


int main() {
    
    
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    while (1)
    {
        ll n;
        cin >> n;
        if (n == 0)
        {
            break;
        }
        vector<ll> w(n);
        for (int i = 0; i < n; ++i)
        {
            cin >> w[i];
        }
        
        dp = vector<vector<ll>>(n, vector<ll>(n, -1));
        
        ll ret = search(0, n - 1, w);
        
        cout << ret * 2 << endl;
    }
    
    

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<iterator>

using namespace std;

typedef vector<int> vct;
queue<vct> q;

int solve() {
	int max = 350;
	while (1) {
		if (q.empty())break;
		vct v = q.front();
		q.pop();

		/*for (int j = 0; j < v.size(); j++) {
			cout << v[j] << " ";
		}
		cout << endl;*/

		if (v.size() >= 2) {
			for (int i = 0; i < v.size() - 1; i++) {
				if (abs(v[i] - v[i + 1]) <= 1) {
					vct vtmp = v;
					//copy(daru.begin(), daru.end(), back_inserter(v1));
					vtmp.erase(vtmp.begin() + i);
					vtmp.erase(vtmp.begin() + i);
					q.push(vtmp);
				}
			}
		}
		if (v.size() < max)max = v.size();
	}
	return max;
}

int main() {

	while (1) {
		int n;
		vct daru;
		cin >> n;

		if (n == 0)break;

		//???????????????????????£??\
		for (int i = 0; i < n; i++) {
			int w;
			cin >> w;
			daru.push_back(w);
		}

		//?§?????????¢?´¢
		for (int i = 0; i < daru.size() - 1; i++) {
			if (abs(daru[i] - daru[i + 1]) <= 1) {
				vct v1 = daru;
				//copy(daru.begin(), daru.end(), back_inserter(v1));
				v1.erase(v1.begin() + i);
				v1.erase(v1.begin() + i);

				/*for (int j = 0; j < v1.size(); j++) {
					cout << v1[j] << " ";
				}
				cout << endl;*/

				q.push(v1);
			}
		}

		if (q.empty()) {
			cout << 0 << endl;
		}
		else {
			cout << n - solve() << endl;
		}
	}
	return 0;
}

C++: #define _GLIBCXX_DEBUG

#include <bits/stdc++.h>


using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = { 0 };
vector<int> w(n);

int rec(int l, int r) {
  if (r - 1 == l) {
    return 0;
  }
  
  if (r - l  - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return 2;
    }
  }
  if (memo[l][r]) {
    return memo[l][r];
  }
  
  int ret = 0;
  bool isOK = false;
  
  if (abs(w[l] - w[r - 1]) <= 1) {
    isOK = true;
  }


  for (int t = l + 1; t < r - 1; t++) {
    int temp2 = rec(l, t) + rec(t, r);
    if ((isOK && temp2 == r - l - 2)) {
      temp2 += 2;
    }
    for (int k = t + 1; k < r; k++) {
      int temp = ((isOK && temp == r - l - 2)? 2 : 0) + rec(t, k) + rec(k, r);
       if ((isOK && temp == r - l - 2)) {
         temp += 2;
       }
       ret = max(ret, max(temp, temp2));   
    }
  }

  return memo[l][r] = ret;
}

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

    w.clear();
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    memset(memo, 0, sizeof(memo));
    cout << rec(0, n) << endl;

  }

  

  return 0;
}