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

int h;

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


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


void exchange(int *A ,int l,int i){
  int temp;

  temp=A[l];
  A[l]=A[i];
  A[i]=temp;
 

}

void maxHeapify(int *A,int i){
  int l = left(i);
  int  r = right(i);
  int largest;
 
  if (l <= h && A[l] > A[i]){
    largest = l;
  }
  else {
    largest = i;
  }
  if(r <= h && A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    exchange(A,largest,i); 
    maxHeapify(A, largest);
    
  }
}

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

int main(){
  
  int i;
  int *A;

  A=(int *)malloc((h+1)*sizeof(int));
  
  
  scanf("%d",&h);
  
  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;
}




 

