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 <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

int main() {
    int N, w[300];
    while (true) {
        cin >> N;
        if (N == 0) break;
        for (int j = 0; j < N; ++j) cin >> w[j];

        int dp[305][305];
        for (int j = 0; j < N; ++j) {
            fill(dp[j], dp[j]+N+1, 0);
            dp[j][j] = 1;
        }

        int ans = 0;
        for (int d = 2; d <= N; d += 2) {
            for (int j = 0; j + d <= N; ++j) {
                if (dp[j][j+d-2] && abs(w[j+d-2] - w[j+d-1]) <= 1) {
                    ans = d;
                    dp[j][j+d] = 1;
                }
                if (dp[j+1][j+d-1] && abs(w[j] - w[j+d-1]) <= 1) {
                    ans = d;
                    dp[j][j+d] = 1;
                }
                if (dp[j+2][j+d] && abs(w[j] - w[j+1]) <= 1) {
                    ans = d;
                    dp[j][j+d] = 1;
                }
            }
        }

        cout << ans << endl;
    }
    return 0;
}

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

int n;
int w[310], dp[310][310];

int rec(int i, int j) {
    if (dp[i][j] >= 0) return dp[i][j];
    if (i >= j) return dp[i][j] = 0;
    int res = 0;
    for (int k = i + 1; k < j; ++k) {
        res = max(res, rec(i, k) + rec(k, j));
    }
    if ((j - i) % 2 == 0 && abs(w[i] - w[j - 1]) <= 1) {
        if (rec(i + 1, j - 1) == j - i - 2) {
            res = max(res, dp[i + 1][j - 1] + 2);
        }
    }
    return dp[i][j] = res;
}

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

C++: #include "bits/stdc++.h"
using namespace std;
int w[300];
int memo[300][300]; //[l,r]
int solve(int l, int r) {
	if (l >= r) return 0;
	if (memo[l][r] != -1) return memo[l][r];
	int ans1 = 0, ans2 = 0;
	for (int i = l; i <= r - 1; i++) {
		int sum = 0;
		sum += solve(l, i);
		sum += solve(i + 1, r);
		ans1 = max(ans1, sum);
	}
	for (int i = l+1; i <= r-1; i++) { //[l+1,i] [i+1,r-1]
		int sum = 0;
		sum += solve(l + 1, i);
		sum += solve(i + 1, r - 1);
		ans2 = max(ans2, sum);
	}
	if (ans2 == (r - l - 1) && abs(w[l] - w[r]) <= 1) {
		ans2 += 2;
	}
	return memo[l][r] = max(ans1,ans2);
}

int main() {
	int n;
	while (scanf("%d", &n), n) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &w[i]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) memo[i][j] = -1;
		}
		printf("%d\n", solve(0, n - 1));
	}
}

C++: #include <iostream>
#include <bitset>
#include <vector>

using namespace std;

vector<vector<int>> mem;
vector<int> bs;

int rec(int l, int r) {
  if(mem[l][r] >= 0) return mem[l][r];
  if(l >= r) {
    mem[l][r] = 0;
  } else {
    int d = r - l + 1;
    if(rec(l+1, r-1) == d - 2 && abs(bs[l] - bs[r]) <= 1) {
      mem[l][r] = d;
    } else {
      int ans = 0;
      for(int k = l; k < r; k++) {
        ans = max(ans, rec(l, k) + rec(k+1, r));
      }
      mem[l][r] = ans;
    }
  }
  return mem[l][r];
}

int main(int argc, char *argv[])
{
  for(;;) {
    int m;
    cin >> m;
    if(m == 0) break;
    bs.clear();
    for(int i = 0; i < m; i++) {
      int b;
      cin >> b;
      bs.push_back(b);
    }
    mem = vector<vector<int>>(m, vector<int>(m, -1));
    int ans = rec(0, m-1);
    cout << ans << endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class Solve {
public:
    int N;
    vector<int> w;

    void input() {
        w.resize(N);
        rep(i, 0, N) cin >> w[i];
    }

    int dp[305][305];

    int rec(int l, int r) {
        if (l >= r) return 0;
        if (dp[l][r] != -1) return dp[l][r];

        if (l + 1 == r) {
            return dp[l][r] = 2 * (abs(w[l] - w[r]) <= 1);
        }
        int ret = 0;
        for (int k = l; k < r; ++k) {
            chmax(ret, rec(l, k) + rec(k + 1, r));
        }
        if (rec(l + 1, r - 1) == r - l - 1 && abs(w[l] - w[r]) <= 1) {
            chmax(ret, rec(l + 1, r - 1) + 2);
        }

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

    void solve(int n) {
        N = n;
        input();
        memset(dp, -1, sizeof(dp));

        int ans = rec(0, n - 1);
        cout << ans << endl;

    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    while (true) {
        int tmp;
        cin >> tmp;
        if (tmp == 0) break;
        Solve().solve(tmp);
    }


    return 0;
}