#include<stdio.h>
#include<stdlib.h>
int parent(int);
int left(int);
int right(int);
void buildMaxHeap(int*,int);
void maxHeapify(int*,int,int);
void swap(int*,int,int);
void printHeap(int*,int);
int main(void)
{
  int i;//counter
  int n;//the size of heap
  int *arr;//array (1-origin)
  scanf("%d",&n);
  arr=(int*)malloc(sizeof(int)*(n+1));
  for(i=1;i<=n;i++) scanf("%d",&arr[i]);
  buildMaxHeap(arr,n);
  printHeap(arr,n);
  return 0;
}
int parent(int i)
{
  return i/2;
}
int left(int i)
{
  return 2*i;
}
int right(int i)
{
  return 2*i+1;
}
void buildMaxHeap(int *arr,int n)
{
  int i;//counter
  for(i=n/2;i>=1;i--) maxHeapify(arr,i,n);
}
void maxHeapify(int *arr,int i,int n)
{
  int l=left(i);
  int r=right(i);
  int largest;
  if(l<=n&&arr[l]>arr[i]) largest=l;
  else largest=i;
  if(r<=n&&arr[r]>arr[largest]) largest=r;
  if(largest!=i)
    {
      swap(arr,i,largest);
      maxHeapify(arr,largest,n);
    }
}
void swap(int *arr,int x,int y)
{
  int temp;
  temp=arr[x];
  arr[x]=arr[y];
  arr[y]=temp;
}
void printHeap(int *arr,int n)
{
  int i;
  for(i=1;i<=n;i++) printf(" %d",arr[i]);
  printf("\n");
}

