import scala.io.StdIn.{readInt, readLine};
import scala.reflect.ClassTag;
object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt; // 要素数
        val arr = readLine.split(" ").map(_.toLong);
        val h = Heap[Long](arr, (a: Long, b: Long) => if (a < b) -1 else if (a > b) 1 else 0);
        h.dump;
    }
}
class Heap[A](heap: Array[Option[A]], compare: (A, A) => Int) {
    val size = heap.length;
    var ptr = 1;
    def dump(): Unit = {
        println(" " + heap.filter(_ != None).map(_.get).mkString(" "));
    }
    def add(key: A): Unit = {
        if (ptr < size) {
            heap(ptr) = Some(key);
            buildHeap();
            ptr += 1;
        }
    }
    
    private def buildHeap(): Unit = {
        def _go(idx: Int): Unit = {
            if (idx < size && heap(idx) != None) {
                _go(idx * 2 + 1);
                _go(idx * 2 + 2);
                heapify(idx)
            }
        }
        _go(0);
    }
    
    private def heapify(idx: Int): Unit = {
        val lIdx = idx * 2 + 1;
        val rIdx = idx * 2 + 2;
        val curr = heap(idx);
        val left = if (lIdx < size) heap(lIdx) else None;
        val right = if (rIdx < size) heap(rIdx) else None;
        if (left != None && right != None) { // 左右の子ノードが存在
            if (compare(curr.get, left.get) < 0 || compare(curr.get, right.get) < 0 ) {
                val sp = if (compare(left.get, right.get) < 0) rIdx else lIdx;
                swap(idx, sp);
                heapify(sp)
            }
        } else if (left != None) { // 左の子ノードのみ存在
            if (compare(curr.get, left.get) < 0) {
                swap(idx, lIdx);
                heapify(lIdx)
            }
        }
    }

    private def swap(idx1: Int, idx2: Int): Unit = {
        val tmp = heap(idx1);
        heap(idx1) = heap(idx2);
        heap(idx2) = tmp;
    }
}

object Heap {
    def apply[A](arr: Array[A], compare: (A, A) => Int): Heap[A] = {
        val h = new Heap[A](arr.map(Option[A]), compare);
        h.buildHeap();
        h
    }
    def apply[A](size: Int, compare: (A, A) => Int)(implicit m: ClassTag[Option[A]]): Heap[A] = {
        val arr = m.newArray(size);
        new Heap[A](arr, compare);
    }
}
