#include<stdio.h>

int a[500000];
int h;

void maxHeapify(int i)
{
  int largest;
  int temp;
  
  if(i*2 <= h && a[i*2] > a[i])largest = i*2;
  else largest = i;
  if(i*2+1 <= h && a[i*2+1] > a[largest])largest = i*2+1;

  if(largest != i)
    {
      temp = a[i];
      a[i] = a[largest];
      a[largest] = temp;

      maxHeapify(largest);
    }

  return;
}

void buildMaxHeap()
{
  int i;
  
  for(i = h/2; i > 0; i--)
    {
      maxHeapify(i);
    }

  return;
}

int main()
{
  int i;
  
  for(i = 0; i < 500000; i++)
    {
      a[i] = NULL;
    }
  
  scanf("%d", &h);
  for(i = 1; i <= h; i++)
    {
      scanf("%d", &a[i]);
    }

  buildMaxHeap();
  
for(i = 1; i <= h; i++)
    {
      printf(" %d", a[i]);
    }
 printf("\n");
  return 0; 
}

