#include <stdio.h>
#define H 5000000
 
int n;
int s[H];
 
void maxHeapify(int);
void buildMaxHeap();
 
int main()
{
  int i;
 
  scanf("%d", &n);
 
  for(i=1;i<=n;i++){
    scanf("%d", &s[i]);
  }
 
  buildMaxHeap();
 
  for(i=1;i<=n;i++)
    printf(" %d", s[i]);
  printf("\n");
 
  return 0;
}
 
void buildMaxHeap()
{
  int i;
  
  for(i=n/2;i>0;i--)
    maxHeapify(i);
}
 
void maxHeapify(int i)
{
  int j, k, max, tmp;
   
  j = i*2;
  k = i*2+1;
  if(j<=n && s[j]>s[i])
    max = j;
  else max = i;
  if(k<=n && s[k]>s[max])
    max = k;
  if(max!=i){
    tmp = s[i];
    s[i] = s[max];
    s[max] = tmp;
    maxHeapify(max);
  }
}