#include<stdio.h>

struct intree{
  int parent;
  int left;
  int right;
  int key;
};

int size;
struct intree list[100000];
int Parent(int i){
   return i/2;
}

int Left(int i){
  return 2*i;
 
}

int Right(int i){
  return 2*i+1;  
 
}

void maxHeapify(int i){
  int left,right,largest;
 struct intree key;
  
left=Left(i);
  right=Right(i);

  if(left<=size && list[left].key>list[i].key)
    largest=left;
  else largest=i;
 
 if(right<=size && list[right].key>list[largest].key)
    largest=right;
 
 if(largest!=i){
    key=list[i];
    list[i]=list[largest];
    list[largest]=key;
    maxHeapify(largest);
  }  
}
void buildMaxHeap(){
  int i;
  for(i=(size/2);i>=1;i--) maxHeapify(i);
}





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