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


void maxheapotf(int size,int array[], int i );
void buildMaxHeap(int size, int array[]);
int main()
{
    int size;
    scanf("%d",&size);
    int array[size+1];
    for(int i = 1; i<=size; i++)
    {
        scanf("%d", &array[i]);
    }  
    buildMaxHeap(size,array);
  
    for(int i=1;i<=size;i++)
    {
        printf(" %d", array[i]);
   
    }     printf("\n");
    
    return 0;
}


void maxheapotf(int size, int array[], int i)
{
    int temp, l, r, largest;
    l = 2 * i;
    r = 2 * i + 1;
    if(l <= size)
    {
        if(array[l] > array[i])
            largest = l;
        if(array[l] < array[i])
            largest = i;
    }
    if(r <= size && array[r] > array[largest])
    {
        largest = r;
    }
    if(largest != i && largest > 0)
    {
        temp = array[i];
        array[i] = array[largest];
        array[largest]= temp; 
        maxheapotf(size, array, largest);
    }
   
}

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

