class PriorityQueue
  #?????????
  def initialize
    @queue = Array.new
  end

  #???????????????????´??????????
  def top
    @queue[0]
  end

  #???????????????????´??????????????????????????´??????????
  def pop
    top = @queue[0]
    n = @queue.size - 1

  end

  #?????§?????´??°
  def max_heap_fy(i)
    l = 2 * i
    r = 2 * i + 1
    if l<= @queue.size && @queue[l] > @queue[i] then largest = l
    else largest = i end
    largest = r if r <= @queue.size && @queue[r] > @queue[largest]
    if largest != i then
      @queue[i],@queue[largest] = @queue[largest],@queue[i]
      max_heap_fy(largest)
    end
  end
end


def max_heap_fy(i)
  l = 2 * i
  r = 2 * i + 1
  if l<= @h && @que[l] > @que[i] then largest = l
  else largest = i end
  largest = r if r <= @h && @que[r] > @que[largest]
  if largest != i then
    @que[i],@que[largest] = @que[largest],@que[i]
    max_heap_fy(largest)
  end
end

@h = gets.to_i
@que = gets.split.map(&:to_i)
@que.insert(0,-114514)
(@h / 2).downto(1)do |i|
  max_heap_fy(i)
end
puts " #{@que[1..@que.size - 1].join(" ")}"
