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

int size;
int A[500001];

void swap(int* a, int* b) 
{
    int tmp = *a;
     *a = *b;
    *b = tmp;
}

void maxheapify(int A[],int i)
 {
     int l = i * 2;
    int r = i * 2 + 1;

    int largest;
    if (l <= size && A[l] > A[i]) largest= l;
    else largest = i;
    if (r <= size && A[r] > A[largest]) largest = r;
    if (largest != i) {
     swap(&A[i], &A[largest]);
     maxheapify(A,largest);
 }


}

void buildmaxheap()
 {
     for (int i = size / 2; i > 0; i--) 
     {
         maxheapify(A,i);
 }

}

    void printing()
{
    int i;
 for (i = 1; i <= size; i++)
  {
      printf(" %d", A[i]);
 }
}
int main() 
{
    int i;
    scanf("%d", &size);

    for (i = 1; i <= size; i++)
     {
         scanf("%d", &A[i]);
 }

 buildmaxheap();
 printing();

 printf("\n");
 return 0;
}

