#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 l, r, max, tmp;
  
  l = i*2;
  r = i*2+1;
  if(l<=n && s[l]>s[i])
    max = l;
  else max = i;
  if(r<=n && s[r]>s[max])
    max = r;
  if(max!=i){
    tmp = s[i];
    s[i] = s[max];
    s[max] = tmp;
    maxHeapify(max);
  }
}