def maxHeapify(a, i)
  h = a.size
  l = 2 * i + 1
  r = 2 * i + 2
  largest = i
  largest = l  if l < h && a[l] > a[i]
  largest = r if r < h && a[r] > a[largest]

  if largest != i then
    a[i], a[largest] = a[largest], a[i]
    maxHeapify(a, largest)
  end
end
def buildMaxHeap(a)
  h = a.size
  (h/2-1).downto(0){|i|
    maxHeapify(a, i)
  }
end

gets.to_i
a = gets.split.map(&:to_i)

buildMaxHeap(a)

puts " #{a.join(" ")}"