#include <stdio.h>
int A[500000];
int h;

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

  if(l <= h && A[l]>A[i]){largest=l;}
  else {largest=i;}

  if(r <= h && A[r]>A[largest]){largest=r;}

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

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

int main(){
  int i;
  scanf("%d",&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;
}
  

