#include <stdio.h>
#define MAX 100000

int H;

void maxHeapify(int *B,int j)
{
  int l,r,largest;
  int temp;
  l = 2 * j;
  r = 2 * j + 1;
  if(l <= H && B[l] > B[j])
    largest = l;
  else
    largest = j;
  if(r <= H && B[r] > B[largest])
    largest = r;
  if(largest != j)
    {
      temp = B[j];
      B[j] = B[largest];
      B[largest] = temp;
      maxHeapify(B,largest);
    }
};

int main()
{
  int i,A[MAX+1];
  
  scanf("%d",&H);
  for(i = 1; i <= H; i++)
    scanf("%d",&A[i]);
  for(i = H/2; i >= 1; i--)
    maxHeapify(A,i);
  for(i = 1;i <= H; i++)
      printf(" %d",A[i]);
  printf("\n");
  
  return 0;
}

