Problem:
Score : 600 points

Problem Statement

We have one slime.

You can set the health of this slime to any integer value of your choice.

A slime reproduces every second by spawning another slime that has strictly less health. You can freely choose the health of each new slime. The first reproduction of our slime will happen in one second.

Determine if it is possible to set the healths of our first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals a multiset S.

Here S is a multiset containing 2^N (possibly duplicated) integers: S_1,~S_2,~...,~S_{2^N}.

Constraints

All values in input are integers.

1 \leq N \leq 18

1 \leq S_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_{2^N}

Output

If it is possible to set the healths of the first slime and the subsequent slimes spawn so that the multiset of the healths of the 2^N slimes that will exist in N seconds equals S, print Yes; otherwise, print No.

Sample Input 1

2
4 2 3 1

Sample Output 1

Yes

We will show one way to make the multiset of the healths of the slimes that will exist in 2 seconds equal to S.

First, set the health of the first slime to 4.

By letting the first slime spawn a slime whose health is 3, the healths of the slimes that exist in 1 second can be 4,~3.

Then, by letting the first slime spawn a slime whose health is 2, and letting the second slime spawn a slime whose health is 1, the healths of the slimes that exist in 2 seconds can be 4,~3,~2,~1, which is equal to S as multisets.

Sample Input 2

2
1 2 3 1

Sample Output 2

Yes

S may contain multiple instances of the same integer.

Sample Input 3

1
1 1

Sample Output 3

No

Sample Input 4

5
4 3 5 3 1 2 7 8 7 4 6 3 7 2 3 6 2 7 3 2 6 7 3 4 6 7 3 4 2 5 2 3

Sample Output 4

No

Solution in Ruby (2.3.3) (judge verdict: Accepted)

#二分ヒープ
class BinaryHeap
  def initialize
    @heap = Array.new
    @size = 0
  end
  def push(x)
    @size += 1
    upfrom(@size - 1, x)
  end
  def shift
    wk = @heap[0]
    @size -= 1
    downfrom(0, @heap[@size])
    wk
  end
  def change(o, n)
    i = @heap.find_index{|h| h == o}
    return if i.nil?
    if i == 0 || (o <= n) then
      downfrom(i, n)
    else
      upfrom(i, n)
    end
  end
  def delete(x)
    i = @heap.find_index{|h| h == x}
    return if i.nil?
    @size -= 1
    downfrom(i, @heap[@size])
  end
  def first
    @size == 0 ? nil : @heap[0]
  end
  def second
    if @size < 2 then
      nil
    elsif @size == 2 then
      @heap[1]
    else
      @heap[1] <= @heap[2] ? @heap[1] : @heap[2]
    end
  end
  def empty?
    @size == 0
  end

  private

  def upfrom(i, x)
    me = i
    while 0 < me do
      parent = (me - 1) >> 1
      break if @heap[parent] <= x
        @heap[me] = @heap[parent]
        me = parent
    end
    @heap[me] = x
  end
  def downfrom(i, x)
    me = i
    while me < @size do
      child = (me << 1) + 1
      break if child >= @size
      if (child + 1) < @size && (@heap[child + 1] <= @heap[child]) then
        child += 1
      end
      break if x <= @heap[child]
      @heap[me] = @heap[child]
      me = child
    end
    @heap[me] = x
  end
end

N = gets.to_i
S = gets.chomp.split(" ").map(&:to_i)

S.sort!.reverse!
max_size = 2 ** N
C = Hash.new{|h, k| h[k] = BinaryHeap.new}
C[S[0]].push(0)
CI = BinaryHeap.new
CI.push(0)
P = Array.new(2 ** (N + 1) - 1)
P[0] = S[0]
ans = true
(1...S.size).each{|i|
  s = S[i]
  j = CI.first
  j = CI.second if P[j] == s
  (ans = false; break) if j.nil?
  k = P[j]
  jj = C[k].shift
  wk = 2 * j + 2
  if wk < max_size then
    C[k].push(wk - 1)
    CI.change(j, C[k].first)
    P[wk - 1] = k
    C[s].push(wk)
    CI.push(wk) if C[s].second.nil?
    P[wk] = s
  else
    (C.delete(k); CI.delete(j)) if C[k].empty?
  end
}

puts ans ? 'Yes' : 'No'