#include <stdio.h>

void maxheapify(int);

int H[5000000];
int n;

int main(){
  int i;

  scanf("%d",&n);

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

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

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

  return 0;
}

void maxheapify(int i){
  int l = 2*i;
  int r = 2*i + 1;
  int largest;
  int temp;

  if(l <= n && H[l] > H[i]){
    largest = l;
  }
  else largest = i;

  if(r <= n && H[r] > H[largest]){
    largest = r;
  }

  if(largest != i){
    temp = H[i];
    H[i] = H[largest];
    H[largest] = temp;
    
    maxheapify(largest);
  }
} 

  

