#include<stdio.h>
#define MAX 500000
int hsize;
int heap[MAX];//
int main(){
  int i,l,r;
  scanf("%d",hsize);
  for(i=1; i <= hsize; i++)//1 から hsizeまで
    scanf("%d",heap[i]);
  make();
  for(i=1;i<=hsize; i++)//1 から hsizeまで
    printf(" %d",heap[i]);
  printf("\n");
  return 0;
}

int maxheap(j){
  int len;
  int l = left(j),r= right(j),m;
  if(l <= hsize  && heap[l] < heap[l])
    len =l;
  else
    len = j;
  if(r <= hsize && heap[r] > heap[len])
    len = r;
  if(len != j){
    m=heap[j];
    heap[j] = heap[len];
    heap[len]=m;
    maxheap(len);
  }
}
int make(){
  int i;
  for(i =(hsize/2); i>= 1; i --){
    maxheap(i);
  }
}int parent(int i){
  int x= i/2;
  return x;
}

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

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