#include<stdio.h>

void maxHeapify(int);

int n, A[2000001];

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 x){
  int right, left, largest;
  int tmp;
  left = 2 * x;
  right = 2 * x + 1;

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

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

  if(largest != x){
    tmp = A[x];
    A[x] = A[largest];
    A[largest] = tmp;

    maxHeapify(largest);
  }
}


