var h,n;

function maxHeapify(i){
    var l = i * 2;
    var r = i * 2 + 1;

    var largest;
    if(l <= n && h[l] > h[i]){
        largest = l;
    }
    else
        largest = i;
    if(r <= n  && h[r] > h[largest])
        largest = r;

    if(largest != i){
        var ex;
        ex = h[i];
        h[i] = h[largest];
        h[largest] = ex;
        maxHeapify(largest);
    }
}

function buildMaxHeap(){
    for(var i = n/*Math.floor((n + 1) / 2)*/;i >= 1;i--)
        maxHeapify(i);
}

function Main(input){
    input = input.split("\n");
    n = parseInt(input[0],10);
    input[1] = input[1].split(" ");
    h = new Array(n + 1);
    for(var i = 1;i <= n;i++)
        h[i] = parseInt(input[1][i - 1],10);

    buildMaxHeap();

    for(var i = 1;i <= n;i++){
        String(h[i]);
    }

    var ans = "";

    for(var i = 1;i <= n;i++){
        ans += " " + h[i];
    }
    console.log(ans);
    
}
 
Main(require("fs").readFileSync("/dev/stdin","utf8"));