#include<stdio.h>

int h,heap[500000];

void maxHeapify(int *,int);
void buildMaxHeap(int *);

int main(){
  int i;
  scanf("%d",&h);
  for(i=1;i<h+1;i++) scanf("%d",heap+i);
  buildMaxHeap(heap);
  for(i=1;i<h+1;i++) printf(" %d",heap[i]);
  printf("\n");
}

void maxHeapify(int *a,int i){
  int l,r,largest,exchange;
  l=i*2;
  r=i*2+1;
  if(l<=h && a[i]<a[l]) largest=l;
  else largest=i;
  if(r<=h && a[largest]<a[r]) largest=r;
  if(largest!=i){
    exchange=a[largest];
    a[largest]=a[i];
    a[i]=exchange;
    maxHeapify(a, largest);
  }
}

void buildMaxHeap(int *a){
  int i;
  for(i=h/2;i>0;i--) maxHeapify(a,i);
}

