#include<stdio.h>

void maxHeapify(int);
void buildMaxHeap();
int A[500001];
int length;

int main(){
  int H,i;

  scanf("%d",&H);
  length=H;
  for(i=1;i<=H;i++)scanf("%d",&A[i]);

  buildMaxHeap();

  for(i=1;i<=H;i++)printf(" %d",A[i]);
  printf("\n");
  return 0;
}

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

}

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

  if(l<=length && A[l]>A[i]) largest=l;
  else largest=i;

  if(r<=length && A[r]>A[largest]) largest=r;

  if(largest!=i){
    a=A[i];
    A[i]=A[largest];
    A[largest]=a;
    maxHeapify(largest);
  } 

}