#include<stdio.h>

#define H 5000001

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

}

void buildMaxHeap(){
	int i;
    for(i = h/2;i>=1;i--){
        maxHeapify(i);
    }
}

int left(int i){
	
	int lid;
    
    lid=2*i;
    //if(lid>h) lid=-1;
    
    return lid;

}

int right(int i){

	int rid;
    
    rid=2*i+1;
    //if(rid>h) rid=-1;
   
    return rid;
    
}

int main(){
	
	int i;
	
	scanf("%d",&h);
	
	for(i=1;i<=h;i++){
		scanf("%d",&A[i]);
	}
	
	buildMaxHeap();
	
	for(i=1;i<=h;i++){
		printf(" %d",A[i]);
	}
	
	printf("\n");
	
	return 0;

}

