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

int A[200000];
int count;

void exchange(int i, int max){
  int kari;
  kari=A[i];
  A[i]=A[max];
  A[max]=kari;
}

void maxHeappify(int i){
  int r,l,max;
  r=2*i;
  l=2*i+1;

  if(l<=count && A[l]>A[i]) max=l;
  else max=i;
  if(r<=count && A[r]>A[max]) max=r;

  if(max!= i){
    exchange(i,max);
    maxHeappify(max);
  }
}


int main(int argc, char const *argv[]) {
    int i;
    scanf("%d",&count);
    for (i = 1; i <= count; i++) {
      scanf("%d",&A[i]);
    }
    for ( i = count/2;i>0; i--) {
      maxHeappify(i);
    }
    for ( i = 1; i <= count; i++) {
      printf(" %d",A[i]);
    }
    printf("\n");

  return 0;
}