class MaximumHeap
  attr_accessor :tree

  def initialize(tree, heap_size)
    @tree = tree
    @heap_size = heap_size
  end

  def max_heapify(i)
    l = 2 * i + 1
    r = 2 * i + 2
    larget = nil
    if l < @heap_size && @tree[l] > @tree[i]
      largest = l
    else
      largest = i
    end
    if r < @heap_size && @tree[r] > @tree[largest]
      largest = r
    end

    if largest != i
      @tree[largest], @tree[i] = @tree[i], @tree[largest]
      max_heapify(largest)
    end
  end

  def build_max_heap
    (@heap_size / 2).downto(0) do |i|
      max_heapify(i)
    end
  end

end

h = gets.to_i
tree = gets.split.map { |i| i.to_i }
mh = MaximumHeap.new(tree, h)
mh.build_max_heap
puts ' ' + mh.tree*' '