#include<stdio.h>
#define H 500000

int h;
int A[H];

void maxHeapify(int A[],int i){
  int l,r,big,c;
  int ch;
  l = i * 2;
  r = l + 1;
  if(l <= h && A[l] > A[i]) big = l;
  else big = i;

  if(r <= h && A[r] > A[big]) big = r;

  if(big != i){
    ch = A[i];
    A[i] = A[big];
    A[big] = ch;
    maxHeapify(A,big);
  }

}

/*void buildMaxHeap(int A[]){
  int i;
  for(i = h/2; i >= 1; i--){
    maxHeapify(A,i);
  }
  }*/

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

  /*buildMaxHeap(A);*/

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

