#include <stdio.h>

int H;

void maxHeapify(int h[],int i){
  int left,right,largest,ex;
  
  left = 2*i;
  right = 2*i+1;

  if(left <= H && h[left] > h[i])largest = left;
  else largest = i;

  if(right <= H && h[right] > h[largest])largest = right;
  
  if(largest != i){
    ex = h[i];
    h[i] = h[largest];
    h[largest] = ex;
    maxHeapify(h,largest);
  }
}

void buildMaxHeap(int h[]){
  int i;

  for(i = H/2; i > 0; i--){
    maxHeapify(h,i);
  }
}

int main(){
  int i;
  int heap[500001];
  
  scanf("%d",&H);

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

