#include <stdio.h>
#define MAX 500000

void swap(int, int);
int parent(int);
int left(int);
int right(int);
void maxHeapify(int);
void buildMaxHeap();

int n;
int A[MAX];

int main()
{
  int i;

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

  buildMaxHeap();

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

  return 0;
}

void swap(int i1, int i2)
{
  int tmp;

  tmp = A[i1];
  A[i1] = A[i2];
  A[i2] = tmp;
}

int parent(int i)
{
  return i / 2;
}

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

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

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

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

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

  if(largest != i)
    {
      swap(i, largest);
      maxHeapify(largest);
    }
}

void buildMaxHeap()
{
  int i;

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