#include<stdio.h>
#define MAX 500001


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


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

return;
}





void buildMaxHeap(int A[],int n){
int i;

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

return;
}





int main(){
int n,i;
int A[MAX];
for(i=1;i<MAX;i++){A[i]=-2000000001;}
scanf("%d",&n);
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;
}
