#include <stdio.h>
int heapsize,A[500001],i;
void buildMapHeap();
void maxHeapify(int );
void print();


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

  buildMapHeap();//create max-heap
  print();
  
  return 0;
}

void buildMapHeap(){
  for(i=heapsize/2;i>=1;i--)
    maxHeapify(i);
}
void maxHeapify(int parent){
  int left,right,maxnode,temp;
  left=2*parent;
  right=left+1;

  if(left<=heapsize && A[left] > A[parent])
    maxnode=left;
  else
    maxnode=parent;
  if(right<=heapsize && A[right] > A[maxnode])
    maxnode=right;

  if(maxnode!=i){
    //need to swap node
    temp=A[maxnode];
    A[maxnode]=A[parent];
    A[parent]=temp;

    if(maxnode*2<=heapsize)
      //there is child of maxnode
      maxHeapify(maxnode);
  }
}
void print(){
  for(i=1;i<=heapsize;i++)
    printf(" %d",A[i]);
  printf("\n");
}

