#include <stdio.h>
#define H 500001

int id;
int a[H];

void maxHeapify(int i){
  int l, r, x, largest;
  l = i*2;
  r = i*2+1;
  if(l <= id && a[l] > a[i]){
    largest = l;
  }else{
    largest = i;
  }
  if(r <= id && a[r] > a[largest]){
    largest = r;
  }

  if(largest != i){
    x = a[i];
    a[i] = a[largest];
    a[largest] = x;
    maxHeapify(largest);
  }
}


int main(){
  int i;

  scanf("%d", &id);

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

  for(i = id/2; i >= 1; i--){
    maxHeapify(i);
  }
  

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

  return 0;
}

