#include <stdio.h>
void swap_node(int H[], int i, int j){
  int tmp=H[i];
  H[i]=H[j];
  H[j]=tmp;
}
void maxHeapify(int H[], int size, int i){
  int left=2*i, right=2*i+1;
  int largest;
  if((left<=size)&&(H[i]<H[left]))
	largest=left;
  else
	largest=i;
  if((right<=size)&&(H[largest]<H[right]))
	largest=right;
  if(largest!=i){
	swap_node(H, i, largest);
	maxHeapify(H, size, largest);
  }
}
void buildMaxHeap(int H[], int size){
  int i;
  for(i=size/2; i>=1; --i){
	maxHeapify(H, size, i);
  }
}
int main(){
  int heap_size;
  int heap[500005];
  int i;

  scanf("%d", &heap_size);
  for(i=1; i<=heap_size; ++i){
	scanf("%d", &heap[i]);
  }
  //ソート
  buildMaxHeap(heap, heap_size);
  //出力
  for(i=1; i<=heap_size; ++i){
	printf(" %d", heap[i]);
}
  printf("\n");
  return 0;
}

