#include<stdio.h>

#define MAX 500000

int A[MAX];
int heap_size;


void maxHeapify(int i){
  int l,r,largest,tmp;
  l=2*i;
  r=l+1;
  if(l<=heap_size && A[l]>A[i])largest=l;
  else largest=i;
  if(r<=heap_size && A[r]>A[largest])largest=r;
  if(largest!=i){
    tmp=A[i];
    A[i]=A[largest];
    A[largest]=tmp;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int i;
  for(i=(int)(heap_size/2);i>=1;i--){
    maxHeapify(i);
  }
}



main(){
  int i;

  scanf("%d",&heap_size);
  for(i=1;i<=heap_size;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap();
  for(i=1;i<=heap_size;i++)printf(" %d",A[i]);
  printf("\n");
  return 0;
}