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

void maxHeapify(int);
void buildMaxHeap(void);

int a[5000000],n;


int main(){

  int i;


  scanf("%d",&n);

  for(i = 1; i <= n; i++){
   
    scanf("%d",&a[i]);
  }

  buildMaxHeap();
 

  for(i = 1; i <= n; i++){
    printf(" %d",a[i]);
  }


  printf("\n");


  return 0;
}


void buildMaxHeap(void){

  int j;

  for(j = n/2; j >= 1; j--){
  
    maxHeapify(j);
  }
}
  
void maxHeapify(int i){

  int left,right,max,tmp;


  left = i*2;
  right = i*2+1;

  if(left <= n && a[left] > a[i]) max = left;


  else max = i;

  if(right <= n && a[right] > a[max])max = right;

  if(max != i){
    tmp = a[i];
    a[i] = a[max];
    a[max] = tmp;
    maxHeapify(max);
  }
}

