#include<stdio.h>

void maxHeapify(int);

int n, A[500005];

int main(){
  int i;

  scanf("%d",&n);

  for(i = 1 ; i <= n ; i++){
    scanf("%d",&A[i]);
  }
  for(i = n / 2 ; i >= 1 ; i--) maxHeapify(i);

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

  return 0;
}

void maxHeapify(int z){
  int right, left, largest;
  int tmp;
  left = 2 * z;
  right = 2 * z + 1;

  if(left <= n && A[left] > A [z]) largest = left;
  else largest = z;

  if(right <= n && A[right] > A[largest]) largest = right;

  if(largest != z){
    tmp = A[z];
    A[z] = A[largest];
    A[largest] = tmp;
    maxHeapify(largest);
  }
}

