#include<stdio.h>
#include<stdlib.h>

void maxHeapify(int i);
void buildMaxHeap(void);
int left(int i);
int right(int i);

int heap[500001];
int n;

int main()
{
  int i;

  scanf("%d",&n);

  for(i = 1; i <= n; i++){
    scanf("%d",&heap[i]);
  }

  buildMaxHeap();

  for(i = 1; i <= n; i++){
    if(i >= 1)printf(" ");
    printf("%d",heap[i]);
  }
  printf("\n");

  return 0;
}

void maxHeapify(int i)
{
  int l,r,largest,temp;

  l = left(i);
  r = right(i);

  if(l <= n && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if(r <= n && heap[r] > heap[largest])
    largest = r;

  if(largest != i){
    temp = heap[i];
    heap[i] = heap[largest];
    heap[largest] = temp;
    maxHeapify(largest);
  } 
}

void buildMaxHeap(void)
{
  int i;

  for(i = n/2; i > 0; i--)
    maxHeapify(i);
}

int left(int i)
{
  return 2 * i;
}

int right(int i)
{
  return 2 * i + 1;
}

