#include<stdio.h>

void max(int *,int);
void build(int *);
int n;  
int main(){
  int i,j,heap[500001];
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&heap[i]);
  }
  build(heap);
  for(i=1;i<=n;i++)
    printf(" %d",heap[i]);
  printf("\n");
  return 0;
}
void build(int *heap){
  int i;
  for(i=n/2;i>0;i--)
    max(heap,i);
}
void max(int *heap,int i){
  int l,r,largest,num;
  l = 2*i;
  r = 2*i+1;
  if(l<=n && heap[l] > heap[i])
    largest = l;
  else
    largest = i;
  if(r<=n && heap[r] > heap[largest])
    largest = r;
  if(largest!=i){
    num=heap[i];
    heap[i]=heap[largest];
    heap[largest]=num;
    max(heap,largest);
  }
}
  

