#include<stdio.h>
#define MAX_NUMBER_OF_NODE 5000001
#define INDEX_OF_ROOT 0
#define DEPTH_OF_ROOT 0



//int node[MAX_NUMBER_OF_NODE];
int number_of_node;//it is represented as H in AOJ

void insert(int);
void print_node(int , int);
void maxHeapify(int);
void buildMaxHeap();
int swap(int);

int node[MAX_NUMBER_OF_NODE];
int number_of_node;

int main()
{
 
  //int number_of_node;//it is represented as H in AOJ
  int i,id;
 
  scanf("%d",&number_of_node);
  for(i = 1 ; i <= number_of_node ; i++)scanf("%d",&node[i]);
  //for(i = 1 ; i < number_of_node ; i++)maxHeapify(i);
  for(i = number_of_node/2 ; i >= 1 ; i--)maxHeapify(i);
  //for(i = 0 ; i <= number_of_node ; i++)buildMaxHeap(void);
  //printf("\nnumber_of_node/2 = %d",number_of_node/2);
  

  //print_node(INDEX_OF_ROOT ,DEPTH_OF_ROOT );
  
  for(i = 1 ; i <= number_of_node ; i++)  printf(" %d",node[i]);
     printf("\n");
    
}


void maxHeapify(int index)
{
  int largest;
  int swap;
  int l = 2*index;
  int r = 2*index+1;

  if(l <= number_of_node && node[l] > node[index])largest = l;
  else largest = index;

  if(r <= number_of_node && node[r] > node[largest]) largest = r;

  if(largest != index )
    {
      swap = node[index];
      node[index] = node[largest];
      node[largest] = swap;
      maxHeapify(largest);
    }

  
  
}







/*
void insert(int index)
{
  int swap;
  if(index == 0)
    {
      
      return;
    }
      
  if(node[index] > node[index/2])
    {
      swap = node[index/2];
      node[index/2] = node[index];
      insert(index/2);
      node[index] = swap;
      return;
    }

}
*/

/*void print_node(int index,int depth)
{

  printf(" %d",node[index]);
  if(index*2 < number_of_index)
  

}
*/




/*#include<stdio.h>
#define MAX_NUMBER_OF_NODE 500000
#define INDEX_OF_ROOT 0
#define DEPTH_OF_ROOT 0

int node[MAX_NUMBER_OF_NODE];
int number_of_node;//it is represented as H in AOJ

void insert(int);
void print_node(int , int);
int swap(int);
int main()
{
  //int node[MAX_NUMBER_OF_NODE];
  //int number_of_node;//it is represented as H in AOJ
  int i,id;


   

  scanf("%d",&number_of_node);
  for(i = 1 ; i <= number_of_node+1 ; i++)
    {
      scanf("%d",&id);
      node[i] = id;
      insert(i);
    }


  //print_node(INDEX_OF_ROOT ,DEPTH_OF_ROOT );
  
  for(i = 1 ; i <= number_of_node ; i++)  printf("%d ",node[i]);
     printf("\n");
    
}

void insert(int index)
{
  int swap;
  if(index == 0) return;
  if(node[index] > node[index/2])
    {
      swap = node[index/2];
      node[index/2] = node[index];
      insert(index/2);
      node[index] = swap;
      return;
    }

}
*/
/*void print_node(int index,int depth)
{

  printf(" %d",node[index]);
  if(index*2 < number_of_index)
  

}
*/

