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

void maxheapify(int*,int,int);
void buildMaxHeap(int*,int);

int main(){
  int i,H,*z;

  scanf("%d",&H);
  z = malloc(H*sizeof(int)+1);
  for(i=1;i <= H;i++){
    scanf("%d",&z[i]);
  }
  buildMaxHeap(z,H);
  for(i=1;i <= H;i++){
    printf(" %d",z[i]);
  }
  printf("\n");
  return 0;
}

void maxheapify(int *z,int i,int H){
  int j,largest;
  if(2*i <= H && z[2*i] > z[i]) largest = 2*i;
  else largest = i;
  if(2*i+1 <= H && z[2*i+1] > z[largest]) largest = 2*i+1;

  if(largest != i){
    j = z[i];
    z[i] = z[largest];
    z[largest] = j;
    maxheapify(z,largest,H);
  }
}

void buildMaxHeap(int *z,int H){
  int i;
  for(i = H/2;i >=1;i--){
    maxheapify(z,i,H);
  }
}

