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++: /*
 * daruma_otoshi_2016D.cpp
 * ICPC2016 Domestic D
 *
 *  Created on: 2017/07/13
 *      Author: LitMc
 */

//#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
#define EPS = 1e-14;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::max;
using std::min;
using std::swap;
using std::string;
using std::fill;
using std::pair;
using std::sort;
using std::reverse;
using std::pair;
using std::greater;
using std::priority_queue;
using std::ostream;

template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i] << endl;
  }
  return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) {
      out << ",";
    }
  }
  out << "]";
  return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

typedef pair<int, int> P;
bool cmp(const P &p1, const P &p2) {
  if (p1.first == p2.first) {
    return p1.second > p2.second;
  } else {
    return p1.first < p2.first;
  }
}

static const int MAX_N = 310;
int n;
bool can[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];
//can[i][j]: w[i]??????w[j]?????§???????????????
//can[i][j] = 1 (if |w[i]-w[j]| <= 1)
//can[i][j] = 1 (if can[i][k] && can[k+1][j])
//dp[i][j]: [i, j]???????????§????????????????????§?????°

vector<int> w;
void init(int n) {
  fill(all(w), 0);
  memset(can, 0, sizeof(can));
  memset(dp, 0, sizeof(dp));
}

bool near(int i, int j) {
  return abs(w[i] - w[j]) <= 1;
}

void debug_can() {
  printf("w[ i]: ");
  rep(i,0,n)
  {
    printf("%2d ", i);
  }
  printf("\n");
  rep(i,0,n)
  {
    printf("w[%2d]: ", i);
    rep(j,0,n)
    {
      printf("%2d ", can[i][j]);
    }
    printf("\n");
  }
}

void doDP_can() {
  rep(i,0,n)
  {
    can[i][i] = false;
  }
  for (int i = 0; i + 1 < n; ++i) {
    if (near(i, i + 1)) {
      can[i][i + 1] = true;
    }
  }
  for (int width = 3; width <= n; ++width) {
    for (int i = 0, j = i + width - 1; j < n; ++i, ++j) {
      if (near(i, j) && can[i + 1][j - 1]) {
        can[i][j] = true;
        continue;
      }
      for (int k = i; k + 1 <= j; ++k) {
        if (can[i][k] && can[k + 1][j]) {
          can[i][j] = true;
          break;
        }
      }
    }
  }
//  debug_can();
}

//dp[i][j]: [i, j]???????????§????????????????????§?????°
//dp[i][j] = j - i + 1 (if can[i][j]==true)
//dp[i][j] = max_k (dp[i][k] + dp[k+1][j])
//????????????[i, j]????????????????±?????????¨???, ????????????????????????????????????????????????????????£???????????°DP??§??????
//width = 2, 3, ... n
//???width?????????[i, j]
void doDP_count() {
  for (int width = 2; width <= n; ++width) {
    for (int i = 0, j = i + width - 1; j < n; ++i, ++j) {
      if (can[i][j]) {
        dp[i][j] = j - i + 1;
        continue;
      }
      int maxi = 0;
      for (int k = i; k + 1 <= j; ++k) {
        maxi = max(maxi, dp[i][k] + dp[k + 1][j]);
      }
      dp[i][j] = maxi;
    }
  }
}

void solve() {
  while (cin >> n, n) {
    w = vector<int>(n);
    init(n);
    rep(i,0,n)
    {
      cin >> w[i];
    }
    doDP_can();
    doDP_count();
    cout << dp[0][n - 1] << endl;
  }
}

int main() {
  solve();
}

C++: #include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

const int MOD_TYPE = 1;
const ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ld DINF = 1e12;
const ld PI = acos(-1.0);
const ld EPS = 1e-11;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;

vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

ll n;
ll w[310];
ll dp[310][310]; //[l,r)で取り除けるブロック

ll calc(int l, int r)
{
  if (dp[l][r] != -1)
    return dp[l][r];
  if (r - l <= 1)
    return 0;

  if (abs(w[l] - w[r - 1]) <= 1 && calc(l + 1, r - 1) == r - l - 2)
  {
    return dp[l][r] = r - l;
  }

  ll Max = 0;
  for (int i = l + 1; i <= r - 1; ++i)
  {
    Max = max(Max, calc(l, i) + calc(i, r));
  }
  return dp[l][r] = Max;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(30) << setiosflags(ios::fixed);
  while (1)
  {
    cin >> n;
    if (n == 0)
      return 0;
    rep(i, n) cin >> w[i];
    rep(i, n + 1) rep(j, n + 1) dp[i][j] = -1;
    cout << calc(0, n) << "\n";
  }
  return 0;
}