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 sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI ( acos(-1.0) )
#define pmod 1000000007
#define maxn 100005
#define IN freopen("input.txt","r",stdin)
#define OUT freopen("output.txt","w",stdout)
#define FOR(i,a,b) for(int i=a ; i<=b ; i++)
#define INF 1000000000
#define ll long long int
#define eps (1e-8)
#define sq(x) ( (x)*(x) )

using namespace std;

typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
int n;
int a[1000];
ll d[1000][1000];
int main()
{while (1){
    FOR (i,1,300){
        FOR (j,1,300){
            d[i][j]=0;
        }
    }
    cin>> n;
    if (n==0) return 0;
    FOR (i,1,n){
        cin>> a[i];
    }
    for (int i=1; i<=n;i++){
        if (sq(a[i]-a[i-1])<=1) d[i-1][i]=0;
        else d[i-1][i]=2;
        d[i][i]=1;
        for (int j=i-2; j>=1;j--){ int u=2;d[j][i]=INF;
            if (sq(a[i]-a[j])<=1) u=0;
            if (d[j+1][i-1]==0){
                d[j][i]=u;
            }
            FOR (k,j,i-1){
                d[j][i]=min(d[j][i],d[j][k]+d[k+1][i]);
            }

        }

    }
    /*FOR(j,1,n){
        FOR(i,1,n){
            cout<<j<<" "<<i<<" "<<d[j][i]<<endl;
        }
    }*/
    cout<<n-d[1][n]<<endl;
}

         return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <regex>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define vi vector<int>

using namespace std;

typedef long long ll;

int main()
{
  while (true)
  {
    int n;
    cin >> n;
    int dp[305][305];
    REP(i, 305)
    {
      REP(j, 305)
      {
        dp[i][j] = 0;
      }
    }
    if (n == 0)
    {
      break;
    }
    vi w = vi(n, 0);
    REP(i, n)
    {
      cin >> w[i];
    }
    for (int width = 2; width < n + 1; width++)
    {
      REP(left, n)
      {
        int right = left + width - 1;
        if (right >= n)
        {
          continue;
        }
        // 間のブロックが全部叩けるかつ両端の重さの差が1以下
        if (dp[left + 1][right - 1] == width - 2 && abs(w[left] - w[right]) <= 1)
        {
          dp[left][right] = width;
        }
        for (int k = left; k < right; k++)
        {
          dp[left][right] = max(dp[left][right], dp[left][k] + dp[k + 1][right]);
        }
      }
    }
    cout << dp[0][n - 1] << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(int i = 0; i < n; i++)




int main() {
    int n;
    
    while(cin >> n, n != 0){
        vector<int> w(n);
        REP(i, n){
            cin >> w[i];
        }
        
        bool dp[305][305] = {};
        REP(i, n - 1){
            if(abs(w[i] - w[i + 1]) <= 1){
                dp[i][i + 1] = true;
            }
        }
        
        for(int len = 3; len <= n; len++){
            for(int l = 0; l < n; l++){
                int r = l + len - 1;
                if(r >= n)continue;

                for(int c = l + 1; c < r - 1; c++){
                    if(dp[l][c] && dp[c + 1][r]){
                        dp[l][r] = true;
                    }
                }
                
                if(dp[l + 1][r - 1] && abs(w[l] - w[r]) <= 1){
                    dp[l][r] = true;
                }
            }
        }
        
        int dp2[305] = {};
        for(int r = 1; r < n; r++){
            dp2[r] = max(dp2[r], dp2[r - 1]);
            REP(l, r){
                if(dp[l][r]){
                    if(l == 0){
                        dp2[r] = max(dp2[r], r - l + 1);
                    }
                    else{
                        dp2[r] = max(dp2[r], dp2[l - 1] + r - l + 1);
                    }
                }
            }
        }
        cout << dp2[n - 1] << endl;
    }
}