#include <stdio.h>
#define MAX 500000

int heap[MAX];
int heap_size;

int parent(int i){
  return i/2;
}
int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
 
void maxHeapify(int i){
  int l, r, L, tmp;
  l=left(i);
  r=right(i);
  if(l <= heap_size && heap[l] > heap[i]){
   L=l;
  }
  else{
   L = i;
  }

  if(r <= heap_size && heap[r] > heap[L]){
   L=r;
  }
  if(L != i){
    tmp=heap[i];
    heap[i] = heap[L];
    heap[L] = tmp;
    maxHeapify(L);
  }
}
 
void builtMaxHeap(void){
  int i;
  for(i = heap_size/2; i > 0; i--){
    maxHeapify(i);
  }
}
 
 
int main(void){
  int i,n;
  scanf("%d",&n);
  heap_size = n;
  for(i = 1; i <= n; i++){
    scanf("%d",&heap[i]);
  }
 
  builtMaxHeap();
 
  for(i = 1; i <= n; i++){
    printf(" %d",heap[i]);
  }
  printf("\n");
  return 0;
}