#include<stdio.h>
#define MAX 500000

void swap(int A[],int x,int y){
int temp;

temp=A[x];
A[x]=A[y];
A[y]=temp;

}


void maxHeapify(int A[],int i,int n){
int l,r,largest;
l=i*2;
r=i*2+1;
if(l<=n&&A[l]>A[i]){largest=l;}
else{largest=i;}
if(r<=n&&A[r]>A[largest]){largest=r;}

if(largest!=i){
 swap(A,largest,i);
 maxHeapify(A,largest,n);
}

return;
}




void buildMaxHeap(int A[],int n){
int i;
 for(i=n/2;i>=1;i--)maxHeapify(A,i,n);

}









int main(){
int n,i;
int A[MAX];

scanf("%d",&n);
for(i=1;i<=2*n;i++)A[i]=-2000000001;
for(i=1;i<=n;i++)scanf("%d",&A[i]);

buildMaxHeap(A,n);

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

return 0;
}
