#include <stdio.h>
#define MAX_LENGTH 500001
void maxHeapify(int arr[], int index, int num)
{
    int largest;int temp;
    int left = 2 * index;
    int right = 2 * index + 1;
    if(left<=num&&arr[left]>arr[index]){largest = left;}
    else{largest = index;}
    if(right<=num&&arr[right]>arr[largest]){largest = right;}
    if(largest!=index)
    {
        temp = arr[index];
        arr[index] = arr[largest];
        arr[largest] = temp;
        maxHeapify(arr, largest, num);
    }
    
}
int main() {
    int num;
    int arr[MAX_LENGTH];
    scanf("%d", &num);
    for(int i = 1;i<num+1;i++){scanf("%d",&arr[i]);}
    for(int i = num/2;i>0;i--){maxHeapify(arr, i, num);}
    for(int j = 1;j<num+1;j++){printf(" %d", arr[j]);}printf("\n");
    return 0;
}

