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 n;
vector<int> w;

int memo[301][301];

int rec(int l, int r) {
  if (r == l) {
    return (memo[l][r] = 1);
  }
  if (memo[l][r] != 0) {
    return memo[l][r];
  }
  if (r - l == 1) {
    if (abs(w[l] - w[r]) <= 1) {
      return (memo[l][r] = 2);
    } else {
      return (memo[l][r] = 1);
    }
  }

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

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

  if (abs(w[r] - w[r - 1]) <= 1) {
    if (rec(l, r - 2) == 2) {
      return (memo[l][r] = 2);    
    }
  }

  return (memo[l][r] = 1);
}


int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    memset(memo, 0, sizeof(memo));

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if ((j - i) % 2 == 0) {
          continue;
        }
        rec(i, j);        
      }
    }

    
    int ans = 0;

    int dp[301] = {0};

    for (int i = 1; i < n; i++) {
      int temp = 0;
      if (memo[0][i] == 2) {
        temp = i + 1;
      }
      dp[i] = max(dp[i - 1], temp);
      for (int j = 1; j < i; j++) {
        if (memo[j][i] == 2) {
          dp[i] = max(dp[i], dp[j - 1] + (i - j + 1));
        }
      }
    }

    ans = dp[n - 1];

    /*
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (memo[i][j] == 2) {
          ans = max(ans, j - i + 1);
        }
        for (int k = i + 1; k <= j; k++) {
          for (int l = k + 1; l < j; l++) {
            if (memo[i][k] == 2 && memo[l][j] == 2) {
              ans = max(ans, (k - i + 1) + (j - l + 1));
            }
          }
        }
      }
      }*/

    cout << ans << endl;
  }


  return 0;
}

C++: // written by @kakira9618
// problem: http://icpc.iisf.or.jp/past-icpc/domestic2016/problems/all_ja.html#section_D

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

int n;
vector<int> W;
int dp[301][301];

// O(n^3)
int dfs(int l, int r) {
    if (r - l <= 1) return dp[l][r] = 0;
    if (dp[l][r] != -1) return dp[l][r]; // ?????¢???
    int ans = 0;
    
    // i????????§??????????????????????¨?????????????????§£??????
    for(int i = l + 1; i < r; i++) {
        ans = max(ans, dfs(l, i) + dfs(i, r));
    }
    
    // ?????£???2??????????????¶?????????1??\???????????????????????¨????¶??????????????????°?????£????????????????¶???????
    if (abs(W[r - 1] - W[l]) <= 1) {
        if (dfs(l + 1, r - 1) == r - l - 2) {
            ans = max(ans, dfs(l + 1, r - 1) + 2);
        }
    }
    return dp[l][r] = ans;
}

int main() {
    while(cin >> n, n) {
        memset(dp, -1, sizeof(int) * 301 * 301);
        W.clear();
        W.resize(n);
        for(int i = 0; i < n; i++) {
            cin >> W[i];
        }
        cout << dfs(0, n) << 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>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


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

void solve(int n) {
    vector<int> w(n);
    rep(i, 0, n) cin >> w[i];
    vector<vector<int> > dp(n, vector<int>(n, 0));
    for (int i = 0; i + 1 < n; i++) {
        if (abs(w[i + 1] - w[i]) <= 1) {
            dp[i][i + 1] = 2;
        } else {
            dp[i][i + 1] = 0;
        }
    }
    for (int d = 3; d < n; d += 2) {
        for (int i = 0; i + d < n; i++) {
            int j = i + d;
            if (abs(w[j] - w[i]) <= 1 && dp[i + 1][j - 1] == j - i - 1) {
                dp[i][j] = j - i + 1;
            } else {
                for (int k = i + 1; k < j; k += 2) {
                    dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
                }
            }

        }
    }
    if (n % 2 == 0) {
        cout << dp[0][n - 1] << endl;
    } else {
        int ans = max(dp[0][n-2],dp[1][n-1]);
        cout << ans << endl;

    }
//    for(int i = 0; i < n; i++){
//
//    }

}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        solve(n);

    }


    return 0;
}