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

int parent(int);
int left(int);
int right(int);
void maxHeapify(int*,int);
void buildMaxHeap(int*);

int h;

int main(){
  int l,r,*A;
  int i;

  scanf("%d",&h);
  A = (int *)malloc(sizeof(int)*(h+1));
  for(i=1;i<=h;i++){
    scanf("%d",&A[i]);
  }

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

  return 0;
}

int parent(int i){
  return i/2;
}

int left(int i){
  return i*2;
}

int right(int i){
  return i*2+1;
}

void maxHeapify(int* A,int i){
  int l,r,largest,tmp;
  
  l = left(i);
  r = right(i);

  if(l<=h && A[l]>A[i]) largest = l;
  else largest = i;

  if(r<=h && A[r]>A[largest]) largest = r;
  
  if(largest != i){
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(A,largest);
  }

  return;
}

void buildMaxHeap(int* A){
  int i;
  
  for(i=h/2;i>0;i--) maxHeapify(A,i);
  return;
}