function maxHeapify(heap, size, id) {
    const left = id * 2;
    const right = id * 2 + 1;
    let largest = id * 2 <= size && heap[left] > heap[id] ? left : id;

    if(id * 2 + 1 <= size && heap[right] > heap[largest]) {
        largest = right;
    }

    if(largest !== id){
        const temp = heap[id];
        heap[id] = heap[largest];
        heap[largest] = temp;
        maxHeapify(heap, size, largest);
    }
}

function buildHeapify(heap, size) {
    let count = Math.floor(size / 2);
    while(count){
        maxHeapify(heap, size, count);
        count--;
    }
}

function main(_stdin){
    const stdin = _stdin.split('\n');
    const heapSize = parseInt(stdin.shift(), 10);
    const _heap = [null];
    const heap = _heap.concat(stdin.shift().split(' ').map(e => parseInt(e, 10)));
    let ans = '';

    buildHeapify(heap, heapSize);
    heap.forEach(e => {
        if (e) ans += ' ' + e;
    });
    console.log(ans)
}


main(require('fs').readFileSync('/dev/stdin', 'utf8'));

