#include <stdio.h>
int A[500001],hs;

int parent(int x){
  return (x/2);
}

int left(int x){
  return 2*x;
}

int right(int x){
  return 2*x+1;
}

void maxHeapify(int i){
  int l,r,tmp;
  int largest;
  l = left(i);
  r = right(i);
  if(l <= hs && A[l] > A[i]){
    largest = l;
  } else {
    largest = i;
  }
  if(r <= hs && A[r] > A[largest]){
    largest = r;
  }
  if(largest !=i){
    tmp = A[i];
    A[i]=A[largest];
    A[largest]= tmp;
    maxHeapify(largest);
  }
  
}

void buildMaxHeap(){
  int i;
  for(i = hs/2;i >0;i--){
    maxHeapify(i);
  }
}

int main(){
  int i;

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

  return 0;
}