#include<stdio.h>
#define MAX 500000

void maxHeapify(int *Heap,int i,int H){
  int l=2*i;
  int r=2*i+1;
  int largest;
  int tmp;

  if(l<=H && Heap[l]>Heap[i]) largest=l;
  else largest=i;
  if(r<=H && Heap[r]>Heap[largest]) largest = r;
  if(largest!=i){
    tmp=Heap[i];
    Heap[i]=Heap[largest];
    Heap[largest]=tmp;
    maxHeapify(Heap,largest,H);
  }
}
void buildMaxHeap(int *Heap,int H){
  int i;
  for(i=H/2;i>=1;i--)
    maxHeapify(Heap,i,H);
}
void print(int *Heap,int H){
  int i;
  for(i=1;i<=H;i++)printf(" %d",Heap[i]);
  printf("\n");
}
  
int main(){
  int n,i;
  int Heap[MAX];
  scanf("%d",&n);
  for(i=1;i<=n;i++)
    scanf("%d",&Heap[i]);
  buildMaxHeap(Heap,n);
  print(Heap,n);
  return 0;
}