#include <stdio.h>
#include <stdlib.h>
void buildMaxHeap(int*);
void maxHeapify(int*,int);
int n = 0;
int main(void){
 int *array;
    int i;
    
    scanf("%d",&n);
    n+=1;
    array = (int *)malloc(n * sizeof(int));
    for(i = 1; i < n; i++){
       scanf("%d",&array[i]);  
    }
    buildMaxHeap(array);
    for(i = 1; i < n; i++){
        printf(" %d",array[i]);
    }
    printf("\n");
    return 0;
}
void maxHeapify(int* array, int i){
    int j,largest,l,r;
    l = i * 2;
    r = 2 * i + 1;
    if(l <= n && array[l] > array[i]){
    largest = l;        
    }
    else largest = i;
    if(r <= n && array[r] > array[largest]){
        largest = r;
    }
    if(largest != i){
        j = array[i];
        array[i] = array[largest];
        array[largest] = j;
        maxHeapify(array,largest);
    }
}
void buildMaxHeap(int* array){
    int i = 0;
    for(i = n/2; i > 0; i--){
        maxHeapify(array,i);
    }
}