#include <stdio.h>

int A[200000000];
int nmber;

void maxHeapify(int i){
    int l, r;
    int  largest, temp;
    

    l = i * 2;
    r = i * 2 + 1;

    if(l <= nmber && A[l] > A[i]) 
        largest = l;
    else 
        largest = i;

    if(r <= nmber && A[r] > A[largest])
        largest = r;
    
    if(largest != i){
        temp = A[i];
        A[i] = A[largest];
        A[largest] = temp;
        maxHeapify(largest);
    }
}

void buildMaxHeap(void){
    int i;
    for(i = nmber/2; i >= 1; i--){
        maxHeapify(i);
    }
}

int main(){
    int i;
  
    scanf("%d", &nmber);

    for(i = 1; i <= nmber; i++) scanf("%d", &A[i]);
    buildMaxHeap();

    for(i = 1; i <= nmber; i++) printf(" %d", A[i]);
    printf("\n");

    return 0;
}

