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


int n;
void maxHeapify(int [], int);
void buildMaxHeap(int []);
int main() {
	int i,H[500001];


	
	scanf("%d",&n);
	
	for(i=1;i<=n;i++) {
		scanf("%d",&H[i]);
		}
	buildMaxHeap(H);	
	for(i=1;i<=n;i++) {
	printf(" %d",H[i]);
	}
	printf("\n");
		return 0;
}
		
	void	  maxHeapify(int A[], int i) {
		int d,largest,l,r;
	
  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) {
     	 d=A[i];
     A[i]= A[largest];   
     A[largest]=d;
          
         maxHeapify(A, largest); 
         }// 再帰的に呼び出し
         }
         
      void   buildMaxHeap(int A[]){
      	int i;
   for( i = n/2; i>=1;  i--)
       maxHeapify(A, i);
       }

