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
}

Python: import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
while True:
    N = int(input())
    if not N:
        break
    W = [int(x) for x in input().split()]
    dp = [[0] * N for _ in range(N)]
    for w in range(2, N + 1):
        for left in range(N - w + 1):
            # [left, right]
            right = left + w - 1
            if right - left - 1 == dp[left + 1][right - 1] and abs(W[left] - W[right]) <= 1:
                dp[left][right] = right - left + 1
                continue
            for mid in range(left + 1, right):
                dp[left][right] = max(dp[left][right], dp[left][mid] + dp[mid + 1][right])
    print(dp[0][N-1])

Python: # coding:utf-8

import sys
from collections import Counter


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


def solve(n, W):
    dp = [[0] * (n + 1) for _ in range(n + 1)]

    def rec(l, r):
        if dp[l][r] != 0:
            return dp[l][r]

        if abs(r - l) <= 1:
            return 0

        res = 0
        if abs(W[l] - W[r - 1]) <= 1 and rec(l + 1, r - 1) == r - l - 2:
            res = r - l

        for mid in range(l + 1, r - 1):
            res = max(res, rec(l, mid) + rec(mid, r))

        dp[l][r] = res
        return res

    return rec(0, n)


while 1:
    n = int(input())
    W = inpl()
    if n == 0:
        exit()
    print(solve(n, W))

Python: from functools import lru_cache
while True:
    N = int(input())
    if N==0:
        break
    W = list(map(int, input().split()))
    assert len(W) == N


    @lru_cache(maxsize=None)
    def solve(l, r):
        assert l<=r
        if (r-l)%2==1:
            return False
        if r-l==2:
            return abs(W[l]-W[l+1])<=1

        if solve(l+1, r-1) and abs(W[l]-W[r-1])<=1:
            return True
        for i in range(l+2, r, 2):
            if solve(l, i) and solve(i, r):
                return True
        return False
        # return (solve(l, r-2) and solve(r-2, r)) \
        #     or (solve(l, l+2) and solve(l+2, r)) \
        #     or


    dp = [0]*(N+1)
    for i in range(1, N+1):
        ma = 0
        for j in range(i-1, -1, -1):
            ma = max(ma, dp[j]+solve(j, i)*(i-j))
            #if (j-i)%2==0:
            #    print(f"j={j}, i={i}, dp={solve(j,i)}")
        dp[i] = ma
    #print(dp)
    print(dp[-1])

Python: import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
ans = []
idx = [x for x in reversed(range(300))]
for i in range(299):
    idx[i + 1] += idx[i]

def dec(l, r):
    return idx[l] + r - l

while True:
    N = int(input())
    if not N:
        break
    W = [int(x) for x in input().split()]
    dp = [0] * (N * (N + 1) // 2)
    for i in range(N-1):
        if abs(W[i] - W[i + 1]) <= 1:
            dp[dec(i, i + 1)] = 2
    for w in range(3, N + 1):
        for left in range(N - w + 1):
            # [left, right]
            right = left + w - 1
            if w - 2 == dp[dec(left + 1, right - 1)] and abs(W[left] - W[right]) <= 1:
                dp[dec(left, right)] = w
                continue
            for mid in range(left + 1, right):
                m = dp[dec(left, mid)] + dp[dec(mid + 1, right)]
                if m > dp[dec(left, right)]:
                    dp[dec(left, right)] = m
    print(dp[dec(0, N-1)])

Python: import sys

while True:
    n=int(input())
    if n==0:
        break
    w = [int(x) for x in sys.stdin.readline().split()]

    dp=[[0]*n for i in range(n)] 

    for l in range(n-1):
        r=l+1
        if abs(w[l]-w[r])<2:
            dp[l][r]=2
        
    for i in range(2,n):
        if i%2==1:
            for l in range(n-i):
                r=l+i
                if dp[l+1][r-1]==r-l-1 and abs(w[l]-w[r])<2:
                    dp[l][r]=r-l+1
                else:
                    for lr in range(l+1,r):
                        if dp[l][r]< dp[l][lr]+dp[lr+1][r]:
                            dp[l][r]=dp[l][lr]+dp[lr+1][r]
        else:
            for l in range(n-i):
                r=l+i
#                dp[l][r]=max(dp[l][r],dp[l+1][r],dp[l][r-1])
                if dp[l][r]<dp[l+1][r]:
                    dp[l][r]=dp[l+1][r]
                if dp[l][r]<dp[l][r-1]:
                    dp[l][r]=dp[l][r-1]
    print(dp[0][n-1])

Python: import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
  while True:
    n = int(input())
    if(n == 0):
      break
    w = list(map(int, input().split()))
    dp = [[0] * (n+1) for _ in range(n+2)]
    for gap in range(2,n+1):
      for l in range(n-gap+1):
        r = l+gap
        if(dp[l+1][r-1] == gap-2):
          if(abs(w[l]-w[r-1]) <= 1):
            dp[l][r] = gap
            continue
        for k in range(l, r):
          if(dp[l][r] < dp[l][k]+dp[k][r]):
            dp[l][r] = dp[l][k]+dp[k][r]
    print(dp[0][n])
    
if __name__ == '__main__':
  main()

Python: def solve(n,w):
    dp=[[0]*(n+1) for _ in range(n+1)]
    for kukan in range(2,n+1):
        for l in range(0,n+1-kukan):
            r=l+kukan
            #decide dp[l][l+kukan]
            if (kukan-2)%2==0:
                if abs(w[l]-w[r-1])<=1 and dp[l+1][r-1]==r-l-2:
                    dp[l][r]=dp[l+1][r-1]+2
                    continue
            for i in range(l+1,r):
                if dp[l][r]<(dp[l][i]+dp[i][r]):
                    dp[l][r]=dp[l][i]+dp[i][r]
    return dp[0][n]
            

n=int(input())
while(n!=0):
    w=list(map(int,input().split()))
    print(solve(n,w))
    n=int(input())

Python: import sys
sys.setrecursionlimit(10000)

def f(daruma):
	global flag, min
	if (flag):
		return

	length = len(daruma)
	if (daruma == 0 or daruma == 1):
		print(n - length)
		flag = true
		return

	for i in range(length-1):
		diff = abs(daruma[i] - daruma[i+1])
		if (diff < 2):
			kari_daruma = daruma[:i] + daruma[i+2:]
			f(kari_daruma)
	else:
		min = (min if min <= length else length)
		return

while 1:
	count = 0
	flag = False
	
	n = int(input())
	if (n == 0):
		break
	min = n
	daruma = list(map(int, input().split()))
	f(daruma)
	print(n - min)