#include<stdio.h>
#define H 500000
int parent(int i);
int left(int i);
int right(int i);
void maxHeapify(int A[],int i);
int parent(int i){
  return i/2;
}

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

int right(int i){
  return 2*i+1;
}
int h;
int A[H];
int main(){
  int i;
  scanf("%d",&h);
  for(i = 1; i <= h; i++) scanf("%d",&A[i]);
  for(i=h/2;i>=1;i--) maxHeapify(A,i);
  for(i=1;i<=h;i++) printf(" %d ",A[i]);
  printf("\n");
  return 0;
}

void maxHeapify(int A[],int i){
  int l,r,largest,x;
  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){
  x=A[i];
  A[i]=A[largest];
  A[largest]=x;
  maxHeapify(A,largest);
  }
}