#include<stdio.h>

#define MAX 500000

int T[MAX];
int size;

void Max_Heapify(int);

int main(){
  int i;
  scanf("%d",&size);

  for(i=1; i<=size; i++){
    scanf("%d",&T[i]);
  }

  for(i=size/2; i>=1; i--) Max_Heapify(i);

  for(i=1; i<=size; i++){
    printf(" %d",T[i]);  
  }
   printf("\n");
  return 0;
}

void Max_Heapify(int i){
  int left, right, largest, temp;
  left = i*2;
  right = i*2+1;

  if(left<=size && T[left]>T[i]) largest = left;
  else largest = i;

  if(right<=size && T[right]>T[largest]) largest = right;

  if(largest != i){
    temp = T[i];
    T[i] = T[largest];
    T[largest] = temp;
    Max_Heapify(largest);
  }
}

