#include <stdio.h>
#define H 500000

int A[H],l;

void maxHeapify(int i){
  int left,right,largest,x;
  left = 2*i;
  right = 2*i+1;
  if(left<=l && A[l]>A[i]){
    largest = left;
  }
  else{
    largest = i;
  }
  if(right<=l && A[right]>A[largest]){
    largest = right;
  }
  if(largest != i){
    x = A[i];
    A[i] = A[largest];
    A[largest] = x;
    maxHeapify(largest);
  }
}

int main(){
  int i;

  scanf("%d",&l);

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

  for(i=l/2 ; i>=1 ; i--){
    maxHeapify(i);
  }

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

  return 0;
}

