import scala.io.StdIn.{readLine,readInt}

object Main {

  def maxHeapify(n:Int, a:Array[Int], i:Int):Unit = {
    var largest = i
    if(i*2 <= n) {
      if(a(i*2-1) > a(i-1)) largest = i*2
      if(i*2+1<=n && a(i*2) > a(largest-1)) largest = i*2+1
      if(largest != i) {
        val temp = a(largest-1)
        a(largest-1) = a(i-1)
        a(i-1) = temp
        maxHeapify(n,a,largest)
      }
    }
  }

  def main(args:Array[String]) = {
    val n = readInt
    val a = readLine.split(" ").map(_.toInt)
    for(i<-n/2 to 1 by -1) {
      maxHeapify(n,a,i)
    }
    println(a.mkString(" "))
  }
}