#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void maxHeapify(int,int[],int);
void buildMaxHeap(int,int[]);

int main(int argc, char** argv) {
    int size;
    scanf("%d", &size);
    int array[size];
    for(int i = 0; i < size; i++)
        scanf("%d",&array[i]);
    
    buildMaxHeap(size,array);
    
    for(int i = 0; i < size; i++)
        printf(" %d",array[i]);
    printf("\n");
    return (EXIT_SUCCESS);
}

void maxHeapify(int size, int array[size], int loc){
    int largest;
    int left = 2*loc+1;
    int right = 2*loc+2;
    int temp;
    if(left < size && array[left] > array[loc])
        largest = left;
    else largest = loc; 
    if(right < size && array[right] > array[largest])
        largest = right;
    if(largest != loc){
        temp = array[loc];
        array[loc] = array[largest];
        array[largest] = temp;
        maxHeapify(size,array,largest);
    }
}

void buildMaxHeap(int size, int array[size]){
    for(int i = size/2; i >= 0; i--)
        maxHeapify(size,array,i);
}


