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){
        vector<int>v(n);
        vector<vector<int>>dp(n+1,vector<int>(n+1,0));
        for(int i=0;i<n;++i){
            cin>>v[i];
        }
        for(int l=2;l<=n;++l){
            for(int i=0;i+l<n+1;++i){
                if(abs(v[i]-v[i+l-1]) < 2 && dp[i+1][i+l-1]==l-2)dp[i][i+l]=l;
                for(int j=1;j<l;++j){
                    dp[i][i+l]=max(dp[i][i+j]+dp[i+j][i+l],dp[i][i+l]);
                }
            }
        }
        cout<<dp[0][n]<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define INF 1000000001
#define ll long long
using namespace std;



int main()
{
  while (true)
  {
    ll n;
    cin >> n;
    if (n == 0)
    {
      break;
    }
    vector<ll> a(n);
    for (ll i = 0; i < n; ++i)
    {
      cin >> a.at(i);
    }
    vector<vector<ll>> dp(n, vector<ll>(n, 0));
    for (ll i = 0; i < n - 1; ++i)
    {
      if (abs(a.at(i + 1) - a.at(i)) <= 1)
      {
        dp.at(i).at(i + 1) += 1;
      }
    }
    for (ll w = 0; w < n; ++w)
    {
      for (ll l = 0; l < n - w; ++l)
      {
        ll r = l + w;
        // cout << "r" << r << endl;
        for (ll m = l; m < r; ++m)
        {
          dp.at(l).at(r) = max(dp.at(l).at(r), dp.at(l).at(m) + dp.at(m + 1).at(r));
        }
        if (l - 1 >= 0 && r + 1 < n)
        {
          // cout << dp.at(l).at(r) << "w" << w << endl;
          // if (dp.at(l).at(r) * 2 == w + 1)
          // {
            //   cout << l << r << endl;
            // }
            if ((dp.at(l).at(r) * 2 == w + 1) && (abs(a.at(l - 1) - a.at(r + 1)) <= 1))
            {
              // cout << l << r << endl;
              dp.at(l - 1).at(r + 1) = dp.at(l).at(r) + 1;
            }
          }
        }
      }
      cout << dp.at(0).at(n - 1) * 2 << endl;
      // for (ll i = 0; i < n; ++i)
      // {
        //   for (ll j = 0; j < n; ++j)
        //   {
          //     cout << dp.at(i).at(j) << ' ';
          //   }
          //   cout << endl;
          // }
  }
}

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

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init,tail...)
    return move(vector<decltype(rec)>(s, rec));
#undef rec
}

const int inf = 1e9;

struct Solver {
    int N;
    vector<int> W;
    vector<vector<int>> dp;

    Solver(int N) : N(N) {
        W.resize(N);
        for(int &e : W) cin >> e;
        dp = make_v(-1, N + 10, N + 10);
    }

    int calc(int l, int r) {
        if(r - l < 2) return 0;
        if(dp[l][r] != -1) return dp[l][r];
        int ret = calc(l + 1, r);
        for(int i = l + 1; i < r; i++) {
            if(abs(W[l] - W[i]) > 1) continue;
            int rec = calc(l + 1, i);
            if(rec < i - (l + 1)) continue;
            int tmp = 2 + rec + calc(i + 1, r);
            ret = max(ret, tmp);
        }
        return dp[l][r] = ret;
    }

    int solve() {
        return calc(0, N);
    }
};

bool solve() {
    int N;
    cin >> N;
    if(!N) return false;
    cout << Solver(N).solve() << endl;
    return true;
}

int main() {
    while(solve());
    return 0;
}