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 <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    while (true) {
        int n;
        cin >> n;

        if (n == 0) {
            return 0;
        }
        vector<int> v;
        for (int i = 0; i < n; ++i) {
            int w;
            cin >> w;
            v.push_back(w);
        }

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

C++: #include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <numeric>
#include <regex>
#include <climits>

#define FOR(i, f, n) for(int (i) = (f), TMPend = (n); (i) < TMPend; (i)++)
#define RFOR(i, f, n) for(int (i) = (f), TMPend = (n); (i) >= TMPend; (i)--)
#define REP(i, n) for(int (i) = 0, TMPend = (n); (i) < TMPend; (i)++)
#define RREP(i, n) for(int (i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for(int (i) = 0, TMPend = (n); (i) < TMPend; (i)++)
#define rrep(i, n) for(int (i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for(auto i: (a))
#define p(...) printf(__VA_ARGS__); cout << endl;
#define in(...) __VA_ARGS__; input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n) x[(n)]; for(int (i) = 0; (i) < (n); (i)++) cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b){ return c ;}
#define key(t, x, compare) [](const t &a, const t &b){ return a x compare b x ;}
#define unless(a) if(!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T>
void input__(T &...a) {
  for(auto v: {&a...}) {
    cin >> *v;
  }
}

void output__(){
  cout << endl;
}

template <class Head, class... T>
void output__(Head const &v, T const &...a) {
  if(sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

int a[300];
bool take[301][301];
bool check[301][301];
int dp[301][301];

bool take_dfs(int i, int j) {
  if (i == j) return false;
  if (check[i][j]) {
    return take[i][j];
  }
  bool isok;
  if (j - i <= 1) {
    isok = abs(a[i] - a[j]) <= 1;
    take[i][j] = isok;
    if (isok) dp[i][j] = j - i + 1;
    check[i][j] = true;
    return isok;
  }

  isok = abs(a[i] - a[j]) <= 1 && take_dfs(i + 1, j - 1);
  FOR(k, i + 1, j) {
    if (isok) break;
    isok = take_dfs(i, k) && take_dfs(k + 1, j);
  }

  take[i][j] = isok;
  if (isok) dp[i][j] = j - i + 1;
  check[i][j] = true;
  return isok;
}

bool f(){
  int input(n);
  if (n == 0) return true;
  memset(a, 0, sizeof(a));
  memset(take, 0, sizeof(take));
  memset(check, 0, sizeof(check));
  memset(dp, 0, sizeof(dp));

  rep(i, n) cin >> a[i];
  rep(i, n - 1) take_dfs(i, n - 1);
  /* rep(left, n - 1) rep(right, n - 1) { */
  /*   , */
  /* } */

  rep(w, n) rep(i, n - w) FOR(k, i, i + w) {
    dp[i][i + w] = max(dp[i][i + w], dp[i][k] + dp[k + 1][i + w]);
  }
  cout << dp[0][n - 1] << endl;

  /* rep(i, n + 1) { */
  /*   rep(j, n + 1) { */
  /*     cout << take[i][j] << " "; */
  /*   } */
  /*   cout << endl; */
  /* } */
  return false;
}

int main() {
  while(1) {
    if(f()) break;
  }
}

C++: #include <cstdio>
#include <cstdlib>

#define int long long
#define dotimes(i, n) for (int i : range(n))

using namespace std;

class range {
  class iterator {
    int i, s;
  public:
    iterator(int i, int s): i(i), s(s) {}
    int& operator*() {
      return i;
    }
    void operator++() {
      i += s;
    }
    bool operator!=(iterator const& that) {
      return i < that.i;
    }
  };
  iterator b, e;
public:
  range(int e): b({0, 1}), e({e, 1}) {}
  range(int b, int e): b({b, 1}), e({e, 1}) {}
  range(int b, int e, int s): b({b, s}), e({e, s}) {}
  iterator begin() {
    return b;
  }
  iterator end() {
    return e;
  }
};

int rint() {
  int n;
  scanf("%lld", &n);
  return n;
}

void wint(int n) {
  printf("%lld\n", n);
}

template<typename T>
inline int size(T container) {
  return static_cast<int>(container.size());
}

template<typename T>
inline bool maxs(T& a, T const& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T>
inline bool mins(T& a, T const& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  for (;;) {
    int n = rint();
    if (!n)
      break;
    int w[n];
    dotimes(i, n)
      w[i] = rint();
    bool dp[n/2+1][n] = {};
    dotimes(i, n)
      dp[0][i] = true;
    for (int i : range(1, n/2+1))
      dotimes(j, n-2*i+1)
        if (abs(w[j] - w[j+2*i-1]) <= 1 && dp[i-1][j+1])
          dp[i][j] = true;
        else
          for (int k : range(1, i))
            if (dp[k][j] && dp[i-k][j+2*k]) {
              dp[i][j] = true;
              break;
            }
    int ls[n+1] = {};
    for (int i = n - 1; i >= 0; i--) {
      ls[i] = ls[i+1];
      dotimes(j, (n-i)/2)
        if (dp[j+1][i])
          maxs(ls[i], 2*(j+1) + ls[i+2*(j+1)]);
    }
    wint(ls[0]);
  }
  return 0;
}