object Main extends App {
  val n: Int = io.StdIn.readInt
  val heap: Array[Int] = io.StdIn.readLine().split(" ").map(_.toInt)
  for (i <- n/2 to 0 by -1) maxHeapify(i)
  println(heap.mkString(" ", " ", ""))

  def maxHeapify(i: Int): Unit = {
    val l: Int = i*2+1
    val r: Int = (i+1)*2
    val larger: Int = if (l < n && heap(l) > heap(i)) l else i
    val largest: Int = if (r < n && heap(r) > heap(larger)) r else larger
    if (largest != i) {
      val tmp: Int = heap(i)
      heap(i) = heap(largest)
      heap(largest) = tmp
      maxHeapify(largest)
    }
  }
}
