#include <stdio.h>
#define N 500000

int data[N+1], n;

void swap(int* i, int* b){
  int tmp = *i;
  *i = *b;
  *b = tmp;
}

void maxHeapify(a){
  int l = 2*a;
  int r = 2*a+1;
  int valM;
  
  if(l <= n && data[l] > data[a]) valM = l;
  else valM = a;
  
  if(r<=n && data[r] > data[valM]) valM = r;
  if(valM != a){
    swap(&data[a], &data[valM]);
    maxHeapify(valM);
  }
}

void buildMaxheap(){
  int a;
  
  for(a=n/2;a>0;a--) maxHeapify(a);
}

main(){
  int a;
  
  scanf("%d", &n);
  
  for(a=1;a<=n;a++) scanf("%d", &data[a]);
  
  buildMaxheap();
  for(a=1;a<=n;a++) printf(" %d", data[a]);
  printf("\n");
  
  return 0;
}

