#include <stdio.h>
void print(int heap[], int heap_size){
  int i;
  int node;
  for(i=1; i<=heap_size; ++i){
	printf("node %d: ", i);
	printf("key = %d, ", heap[i]);
	if(i!=1)
	  printf("parent key = %d, ", heap[i/2]);
	if(2*i<=heap_size)
	  printf("left key = %d, ", heap[2*i]);
	if((2*i+1)<=heap_size)
	  printf("right key = %d, ", heap[2*i+1]);
	printf("\n");
  }
 
}

/**************************************************
 *2つのノードのキーを入れ替える
 *@heap: 入れ替える対象の二分木
 *@i: 二分木の添字
 *@j: 二分木の添字
 *添字iとjのノードを入れ替える
 **************************************************/
void swap_node(int heap[], int i, int j){ 
  int tmp;
  tmp=heap[i];
  heap[i]=heap[j];
  heap[j]=tmp;
}
void max_heapify(int heap[], int heap_size, int i){
  int left=2*i, right=2*i+1;
  int largest;
  if((left<=heap_size)&&(heap[left]>heap[i]))
	largest=left;
  else
	largest=i;
  if((right<=heap_size)&&(heap[right]>heap[largest]))
	largest=right;
  
  if(largest!=i){
	swap_node(heap, largest, i);
	max_heapify(heap, heap_size, largest);
  }
}
void build_max_heap(int heap[], int heap_size){
  int node;
  for(node=(heap_size)/2; node>=1; --node){
	max_heapify(heap, heap_size, node);
  }  
}
int main(){
  int heap_size;
  int heap[256];
  int i;
  //入力
  scanf("%d", &heap_size);
  for(i=1; i<=heap_size; ++i){
	scanf("%d", &heap[i]);
  }
  //maxヒープの作成
   build_max_heap(heap, heap_size);
  //出力
   for(i=1; i<=heap_size; ++i){
	 printf(" %d", heap[i]);
}
   printf("\n");
  return 0;
}

