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

void maxHeapify(int A[],int i);
void buildMaxHeap(int A[]);
 
 
 
int main()
{
int A[500000],h;
int i;
scanf("%d",&h);
for(i=0;i<h;i++)
{
scanf("%d",&A[i]);
}
 
buildMaxHeap(A);
 
for(i=0;i<h;i++)
{
printf(" %d",A[i]);
}
printf("\n");
return 0;
 
}
 
 
 
 
 
 
 
 
 
 
void buildMaxHeap(int A[])
{
int i;
for(i=h/2;i>=0;i--)
{
maxHeapify(A,i);
}
}
 
 
 
 
void maxHeapify(int A[],int i)
{
int l,r,largest,temp;
l=2*1;
r=2*i+1;
if(l<=h&&A[l]>A[i])
{
largest = l;
}
 
else largest = i;
 
if(r<=h&&A[r]>A[largest])
{
largest = r;
}
 
if(largest != i)
{
temp=A[i];
A[i]=A[largest];
A[largest]=temp;
maxHeapify(A,largest);
}
 
 
 
 
}