#include<stdio.h>
#define MAX 2000000

int Si, M[MAX+1];

void maxHeapify(int i){
  int o, t, largest, e;
  o = 2*i;
  t = 2*i+1;
  if ( o <=Si && M[o] > M[i] ) largest = o;
  else largest = i;
  if ( t <= Si && M[t] > M[largest] ) largest = t;
  if ( largest != i ){
    e = M[i]; M[i] = M[largest]; M[largest] = e;
    maxHeapify(largest);
  }
}

main(){
  scanf("%d", &Si);
  int i;
  for ( i =1; i<=Si;i++ ) scanf("%d", &M[i]);
  
  for (i=Si/2;i>=1;i-- ) maxHeapify(i);

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

  
  return 0;
}

