#include<stdio.h>
#define N 500000
int a;

void maxHeapify(int *A,int i);
void buildMaxHeap(int *A);

int main(){
  
  int B[N+1];
  int i;
  
  scanf("%d",&a);
  
  for(i=1;i<=a;i++) scanf("%d",&B[i]);
  
  buildMaxHeap(B);

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

  return 0;
}

void maxHeapify(int *A,int i){
  
  int c;
  int l,r,largest;
  
  l = 2*i;
  
  r = 2*i+1;

  if(l<=a && A[l] > A[i]) largest = l;
  
  else largest = i;
  
  if(r<=a && A[r]>A[largest]) largest = r;
  
  if(largest != i){
    
    c = A[i];
    
    A[i] = A[largest];
    
    A[largest] = c;
    
    maxHeapify(A,largest);
    
  } 
}

void buildMaxHeap(int *A){
  
  int i;

  for(i=a/2;i>=1;i--) maxHeapify(A,i);
  
}

