def put(table, i, k)
  result = Hash.new(0)
  (i+1...k).each do |j|
    left = table[[i, j]]
    right = table[[j, k]]
    left.each do |x, v|
      result[x] = v if v > result[x]
      if right.has_key?(x)
        w = right[x]
        result[x*2] = v+w if v+w > result[x*2]
      end
    end
    right.each do |x, w|
      result[x] = w if w > result[x]
    end
  end
  table[[i, k]] = result
end

def cut(sequence)
  table = {}
  sequence.each_with_index do |x, i|
    table[[i, i+1]] = {x => 1}
  end
  (2..sequence.size).each do |s|
    (0..sequence.size-s).each do |i|
      put(table, i, i+s)
    end
  end
  table
end

def f(sequence)
  cut(sequence)[[0, sequence.size]].values.max
end

loop do
  n = gets.to_i
  break if n == 0
  sequence = gets.split.map!(&:to_i)
  puts f(sequence)
end