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

object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt;
        val arr = readLine.split(" ").map(_.toInt);
        build(n, arr);
        println(" " + arr.mkString(" "));
    }
    
    def build(length: Int, arr: Array[Int]): Unit = {
        def go(curr: Int): Unit = {
            if (curr < length) {
                go(curr * 2 + 1);
                heapify(length, arr, curr);
                go(curr * 2 + 2);
            }
        }
        go(0);
    }
    
    def heapify(length: Int, arr: Array[Int], idx: Int): Unit = {
        val left = idx * 2 + 1;
        val right = idx * 2 + 2;
        if (right < length) {
            val m = max(arr, idx, left, right);
            if (idx != m) {
                swap(arr, idx, m);
                heapify(length, arr, m);
            }
        } else if (left < length) {
            if (arr(left) > arr(idx)) swap(arr, left, idx);
        } else {
            // nop
        }
    }
    
    def max(arr: Array[Int], k1: Int, k2: Int, k3: Int): Int = {
        val v1 = arr(k1); val v2 = arr(k2); val v3 = arr(k3);
        if (v1 > v2) {
            if (v1 > v3) k1 else k3;
        } else {
            if (v2 > v3) k2 else k3;
        }
    }
    
    def swap(arr: Array[Int], a: Int, b: Int): Unit = {
        val tmp = arr(a);
        arr(a) = arr(b);
        arr(b) = tmp;
    }
}
