Problem:
Daruma Otoshi

You are playing a variant of a game called "Daruma Otoshi (Dharma Block Striking)".

At the start of a game, several wooden blocks of the same size but with varying weights
are stacked on top of each other, forming a tower.
Another block symbolizing Dharma is placed atop.
You have a wooden hammer with its head thicker than the height of a block,
but not twice that.

You can choose any two adjacent blocks, except Dharma on the top,
differing at most 1 in their weight,
and push both of them out of the stack with a single blow of your hammer.
The blocks above the removed ones then fall straight down,
without collapsing the tower.

You cannot hit a block pair with weight difference of 2 or more,
for that makes too hard to push out blocks while keeping the balance of the tower.
There is no chance in hitting three blocks out at a time,
for that would require superhuman accuracy.

The goal of the game is to remove as many blocks as you can.
Your task is to decide the number of blocks that can be removed
by repeating the blows in an optimal order.

Figure D1. Striking out two blocks at a time

In the above figure, with a stack of four blocks weighing 1, 2, 3, and
1, in this order from the bottom, you can hit middle
two blocks, weighing 2 and 3, out from the stack. The blocks above will
then fall down, and two blocks weighing 1 and the Dharma block will remain.
You can then push out the remaining pair of weight-1 blocks after that.

Input

The input consists of multiple datasets.
The number of datasets is at most 50.
Each dataset is in the following format.

n

w1 w2 … wn

n is the number of blocks, except Dharma on the top.
n is a positive integer not exceeding 300.
wi gives the weight of the i-th block counted from the bottom.
wi is an integer between 1 and 1000, inclusive.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, output in a line the maximum number of blocks you can remove.

Sample Input

4
1 2 3 4
4
1 2 3 1
5
5 1 2 3 6
14
8 7 1 4 3 5 4 1 6 8 10 4 6 5
5
1 3 5 1 3
0

Output for the Sample Input

4
4
2
12
0

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(int I = int(A); I < int(B); ++I)
#define FORR(I,A,B) for(int I = int((B)-1); I >= int(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;




int main(){
	int N;
	while(1){
		cin >> N;
		if(N==0)break;
		vector<int> w(N+1);
		FOR(i,1,N+1)cin >> w[i];
		bool dp[N+1][N+1]={};
		int ans = 0;
		for(int i=2;i<N+1;i+=2){//i:幅
			FOR(j,1,N+1){//j:開始位置
				// [j,j+i-1]
				if(j+i-1>N)continue;
				if( (j+1 > j+i-2) && abs(w[j]-w[j+i-1])<=1){
					dp[j][j+i-1]=true;
					ans = max(ans,i);
				}else if(dp[j+1][j+i-2] && abs(w[j]-w[j+i-1])<=1){
					dp[j][j+i-1]=true;
					ans = max(ans,i);
				}
				for(int k=2;k<=i-2;k+=2){
					if(dp[j][j+k-1]&&dp[j+k][j+i-1]){
						dp[j][j+i-1]=true;
						ans = max(ans,i);
					}
				}
			}
		}
		int dpa[N+1][N+1]={};
		FOR(i,1,N)if(dp[i][i+1])dpa[i][i+1]=2;
		FOR(i,3,N+1){//幅
			FOR(j,1,N+1){//開始
				if(j+i-1>N)continue;
				for(int k=0;k<i;k++){
					dpa[j][j+i-1] = max(dpa[j][j+k-1]+dpa[j+k][j+i-1],dpa[j][j+i-1]);
					if(dp[j][j+i-1])dpa[j][j+i-1] = i;
				}
			}
		}
		cout << dpa[1][N] << endl;
	}
}