#include <stdio.h>
#define H 500000

int data[H];

void maxHeapify(int,int);

int main(){
  int h,i;

  scanf("%d",&h);
  for(i = 1;i < h+1;i++){
    scanf("%d",&data[i]);
  }
  for(i = h/2;i >= 1;i--) maxHeapify(i,h);
  for(i = 1;i < h+1;i++){
    printf(" %d",data[i]);
  }
  printf("\n");
  
  return 0;
}

void maxHeapify(int i,int h){
  int largest,put;
  
  if(2*i <= h && data[2*i] > data[i]) largest = 2*i;
  else largest = i;
  if(2*i+1 <= h && data[2*i+1] > data[largest]) largest = 2*i+1;

  if(largest != i){
    put = data[i];
    data[i] = data[largest];
    data[largest] = put;
    maxHeapify(largest,h);
  }
  
}

