#include <stdio.h>

#define M 500010
int heap[M];
int heapsize;

int parent(int);
int left(int);
int right(int);
void maxheap(int);
void bmaxheap(void);

int main(int argc,char *argv[]){
  int i,n;
  scanf("%d",&n);
  heapsize=n;
  for(i=1;i<=n;i++){
    scanf("%d",&heap[i]);
  }
 
  bmaxheap();
 
  for(i=1;i<=n;i++){
    printf(" %d",heap[i]);
  }
  printf("\n");
  return 0;
}

int parent(int i){
  return i/2;
}
 
int left(int i){
  return 2*i;
}

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

void maxheap(int i){
  int l,r,largest,tmp;
  l=left(i);
  r=right(i);
  if(l<=heapsize && heap[l]>heap[i]) largest=l;
  else largest=i;
 
  if(r<=heapsize && heap[r]>heap[largest])  largest=r;
   
  if(largest!=i){
    tmp=heap[i];
    heap[i]=heap[largest];
    heap[largest]=tmp;
    maxheap(largest);
  }
}
 
void bmaxheap(void){
  int i;
  for(i=heapsize/2;i>0;i--){
    maxheap(i);
  }
}

