#include<stdio.h>
#define N 5000000

typedef struct
{
  int l,r,key;
}H;

H B[N];

void maxHeapify(int);
void BMH();

int n;

int main()
{
  int i,j;
  scanf("%d",&n);
  B[0].key = 0;
  for(i=1;i<=n;i++)
  {
    scanf("%d",&B[i].key);
    B[i].l = B[i].r = 0;
  }

  BMH();

  for(i=1;i<=n;i++)
  {
    printf(" %d",B[i].key);
  }
  printf("\n");

  return 0;
}

void maxHeapify(int i)
{
  int left=0,right=0,largest,temp;
  left = 2*i;
  right = 2*i+1;

  if(left <= n && B[left].key > B[i].key)
    largest = left;
  else
    largest = i;

  if(right <= n && B[right].key > B[largest].key)
    largest = right;

  if(largest != i)
  {
    temp = B[i].key;
    B[i].key = B[largest].key;
    B[largest].key = temp;
    maxHeapify(largest);
  }
}

void BMH()
{
  int i;
  for(i=n/2;i>=1;i--)maxHeapify(i);
}

