output_description: For each dataset, output in a line the maximum number of blocks you can
  remove.
input_description: The input consists of multiple datasets. The number of datasets is at most 50.
  Each dataset is in the following format.
  
  _n_  
  _w_ 1 _w_ 2 … _w_ _n_  
  
  _n_ is the number of blocks, except Dharma on the top. _n_ is a positive
  integer not exceeding 300. _w_ _i_ gives the weight of the _i_ -th block
  counted from the bottom. _w_ _i_ is an integer between 1 and 1000, inclusive.
  
  The end of the input is indicated by a line containing a zero.
attempt: n = int(input())
  w = list(map(int, input().split()))
  dp = [[-1 for j in range(n + 1)] for i in range(n + 1)]
  
  
  def rec(l, r):
      if dp[l][r] != -1:
          return dp[l][r]
  
      if abs(l - r) <= 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
  
  
  print(rec(0, n))
  
problem_description: 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.
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_D1)
  
  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.
samples: [{"input": "1 2 3 4\n    4\n    1 2 3 1\n    5\n    5 1 2 3 6\n    14\n    8 7 1 4 3 5 4 1 6 8 10 4 6 5\n    5\n    1 3 5 1 3\n    0", "output": "4\n    2\n    12\n    0"}]