#include<stdio.h>
#include<stdlib.h>

#define H 500001

int A[H],h;

void maxHeapify(int he){
  int l,r,i,j,greatest;

  l = 2*he;
  r = 2*he+1;

  if(l<=h && A[l] > A[he]) greatest = l;
  else greatest = he;

  if(r<=h && A[r]> A[greatest]) greatest = r;


  if(greatest != he){
    j = A[greatest];
    A[greatest] = A[he];
    A[he] = j;
    maxHeapify(greatest);
  }
}
  
void buildMaxHeap(){
  int i;
 
  for(i=h/2;i>=1;i--) {
    maxHeapify(i);
 
  }
}

int main(){

  int i,j,k;

  scanf("%d",&h);

  for(i=0;i<h;i++){
    scanf("%d",&A[i+1]);
  }


  buildMaxHeap();

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

