class Maximum
  def initialize
    @arr = []
    monky
  end
  def monky
    def @arr.[](i)
      super(i - 1)
    end
    def @arr.[]=(i, value)
      super(i - 1, value)
    end
  end
  def buildHeap(root = 1)
    left, right = root * 2, root * 2 + 1
    if @arr[right]
      buildHeap(left); buildHeap(right)
      max = [[@arr[left], left], [@arr[right], right]].max
      if max[0] > @arr[root]
        @arr[root], @arr[max[1]] = @arr[max[1]], @arr[root]
        rebuildHeap(max[1])
      end
    elsif @arr[left]
      buildHeap(left)
      if @arr[left] > @arr[root]
        @arr[root], @arr[left] = @arr[left], @arr[root]
        rebuildHeap(left)
      end                                                       
    end
  end
  def rebuildHeap(root = 1)
    left, right = root * 2, root * 2 + 1
    if @arr[right]
      max = [[@arr[left], left], [@arr[right], right]].max
      if max[0] > @arr[root]
        @arr[root], @arr[max[1]] = @arr[max[1]], @arr[root]
        rebuildHeap(max[1])
      end
    elsif @arr[left]
      if @arr[left] > @arr[root]
        @arr[root], @arr[left] = @arr[left], @arr[root]
        rebuildHeap(left)
      end
    end      
  end
  def insert(array)
    @arr = array
    monky
    buildHeap
  end
  def to_s
    @arr.join(" ")
  end
end
def main(input = STDIN)
  heap = Maximum.new
  input.gets
  heap.insert(input.gets.split(" ").map(&:to_i))
  puts " " + heap.to_s
end
main