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 dfs(int w[300],int n){
  int b[300],maxx=0,a;
 /* for(int i=0;i<n;i++)
    cout<<w[i]<<" ";
  cout<<endl;*/
  if(n<2)
    return 0;
  if(n==2&&w[0]-w[1]<=1&&w[0]-w[1]>=-1)
    return 2;
  for(int i=0;i<n-1;i++){
    if(w[i]-w[i+1]<=1&&w[i]-w[i+1]>=-1){
      for(int j=0;j<n;j++){
        if(j<i)
          b[j]=w[j];
        else if(j>i+1)
          b[j-2]=w[j];
      }
      a=dfs(b,n-2)+2;
      if(maxx<a)
        maxx=a;
    }
  }
  return maxx;
}

int main(){
  int n,w[300],maxx;
  cin>>n;
  while(n!=0){
    for(int i=0;i<n;i++)
      cin>>w[i];
    maxx=dfs(w,n);
    cout << maxx<<endl;
    cin>>n;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)

int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0&& m==0) break;
    int a[1000];
    lp(i,n){
      cin>>a[i];
    }
    sort(a,a+n);
    reverse(a,a+n);
    int ans=0;
    lp(i,n){
      if((i+1)%m==0) continue;
      else ans+=a[i];
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<(S)<<endl
using namespace std;

int main(){
	int n;
	while(cin>>n,n){
		int w[310];
		int dp[310][310]={};
		REP(i,0,n) cin>>w[i];
		for(int k=1;k<n;k++){
			for(int i=0;i+k<n;i++){
				if(dp[i+1][i+k-1]==k-1&&abs(w[i]-w[i+k])<=1)
					dp[i][i+k]=dp[i+1][i+k-1]+2;
				for(int j=i;j<i+k;j++)
					dp[i][i+k]=max(dp[i][i+k],dp[i][j]+dp[j+1][i+k]);
			}
		}
		p(dp[0][n-1]);
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
using namespace std;

#define FOR(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) FOR((i),0,(n))

typedef long long ll;

const int INF=1e9+7;

const int MAX_N=305;

int data[MAX_N];

int take[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];

int abs(int x){
	if(x>0)return x;
	else return -1*x;
}

int dfs(int i,int j){
	if(take[i][j]!=-1)return take[i][j];
	take[i][j]=0;
	if(i==j)return 0;
	else if(i+1==j){
		if(abs(data[i]-data[j])<=1)take[i][j]=1;
		else take[i][j]=0;
	}
	else{
		if(dfs(i+1,j-1)==1 && abs(data[i]-data[j])<=1)take[i][j]=1;
		for(int k=i;k<j;k++)if(dfs(i,k)==1 &&dfs(k+1,j)==1)take[i][j]=1;
	}
	return take[i][j];
}

int solve(int i,int j){
	if(dp[i][j]!=-1)return dp[i][j];
	dp[i][j]=0;
	if(dfs(i,j)==1){
		dp[i][j]=j-i+1;
	}
	for(int k=i;k<j;k++)dp[i][j]=max(dp[i][j],solve(i,k)+solve(k+1,j));
	return dp[i][j];
}

int main(){
	int n;
	while(cin>>n && n){
		rep(i,n+1){
			fill(dp[i],dp[i]+MAX_N,-1);
			fill(take[i],take[i]+MAX_N,-1);
		}
		rep(i,n)cin>>data[i+1];
		cout<<solve(1,n)<<endl;
	}
}

C++: #line 1 "../lib/template.hpp"


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>

using namespace std;

#line 2 "tmp.cpp"

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

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vi a(n);
    REP(i, n) { cin >> a[i]; }
    vector<vi> dp(n + 1, vi(n + 1, 0));
    FOR(k, 1, n + 1) {
      REP(l, n + 1 - k) {
        int r = l + k;
        if (k == 1) {
          dp[l][r] = 0;
        } else if (k == 2) {
          dp[l][r] = abs(a[l] - a[r - 1]) <= 1 ? 2 : 0;
        } else {
          if (dp[l + 1][r - 1] == r - l - 2 && abs(a[l] - a[r - 1]) <= 1) {
            dp[l][r] = r - l;
          } else {
            FOR(i, l, r) { dp[l][r] = max(dp[l][r], dp[l][i] + dp[i][r]); }
          }
        }
      }
    }

    cout << dp[0][n] << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;

int w[400];
int memo[400][400];

int max(int i, int j){
	int max = 0;
	for(int k = i ; k < j ; k++){
		int a = memo[i][k] + memo[k+1][j];
		if(a > max){
			max = a;
		}
	}
	if((j - i) % 2 == 1){
		if(w[i] - w[j] <= 1 && w[j] - w[i] <= 1 && memo[i+1][j-1] == j - i - 1){
//			cout << "hit!\n";
			max += 2;
 		}
	}
	return max;
}

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

		if(n == 0) break;
		for(int k = 0 ; k < n ; k++){
			cin >> w[k];
//			cout << "w[" << k << "] = " << w[k] << "\n";
		}

		for(int k = 0 ; k < n - 1; k++){
			memo[k][k] = 0;
//			cout << "memo[" << k << "][" << k << "] = " << memo[k][k] << "\n";
		}

		for(int k = 0 ; k < n - 1; k++){
			memo[k][k+1] = (w[k] - w[k+1] <= 1 && w[k+1] - w[k] <= 1) ? 2 : 0;
//			cout << "memo[" << k << "][" << k + 1 << "] = " << memo[k][k + 1] << "\n";
		}

		for(int b = 2 ; b < n ; b++){ //???
			for(int k = 0 ; k < n - b ; k++){
				memo[k][k + b] = max(k, k + b);
//				cout << "memo[" << k << "][" << k + b << "] = " << memo[k][k + b] << "\n";
			}
		}
//		cout << "memo[" << 0 << "][" << n - 1 << "] = " << memo[0][n-1] << "\n";
		cout << memo[0][n-1] << "\n";
	}
		return 0;
}