H = gets.to_i

def max_heapify(a, i)
  left = i * 2
  right = left + 1
  largest = (left <= H && a[left] > a[i]) ? left : i
  largest = right if right <= H && a[right] > a[largest]
  if largest != i
    a[i], a[largest] = a[largest], a[i]
    max_heapify(a, largest)
  end
end

a = [nil] * 500000
gets.split().each.with_index(1) do |n, i|
  a[i] = n.to_i
end

(H / 2).downto(1) do |i|
  max_heapify(a, i)
end

a[1 .. H].each do |n|
  print " #{n}"
end
puts

