#include <stdio.h>
int a[500001],n;

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

void maxHeapify(int a[],int i){
  int l,r,max=0,t;
  l=left(i);
  r=right(i);
  if(l<=n && a[l]>a[i])max=l;
  else max=i;
  if(r<=n && a[r]>a[max])max=r;

  if(max!=i){
    t=a[i];
    a[i]=a[max];
    a[max]=t;
    maxHeapify(a,max);
  }
}
void buildMaxHeap(int a[]){
  int i;
  for(i=n/2;i>=1;i--)maxHeapify(a,i);
}

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

