#include<stdio.h>
#define N 500004

int h[N], n;

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

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


void maxheapify(i){
  int largest=0,tmp;
  int l,r;
  l = left(i);
  r = right(i);
  if(i <= n && h[l] > h[i]){
    largest = l;
  }
  else
    largest = i;
  if(r <= n && h[r] > h[largest]){
    largest = r;
  }
  if(largest != i){
    tmp = h[i];
    h[i] = h[largest];
    h[largest] = tmp;
    maxheapify(largest);
  }
}

void buildmaxheap(){
  int i;
  for(i = n/2; i>=1; i--){
    maxheapify(i);
  }
}    
  

main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<n+1;i++){
    scanf("%d",&h[i]);
  }
  buildmaxheap();
  for(i=1;i<n+1;i++){
    printf(" %d",h[i]);
  }
  printf("\n");
  return 0;
}
  