#include <stdio.h>
#define H 500000

void maxHeapify(int *, int);

int h;

int main()
{
  int i, a[H];

  scanf("%d",&h);
  for(i=0;i<h;i++){
    scanf("%d",&a[i]);
  }


  for(i=H/2-1;i>=0;i--){
    maxHeapify(a, i);
  }
 
  for(i=0;i<h;i++){
    printf(" %d",a[i]);
  }
  printf("\n");

    return 0;
}

void maxHeapify(int *A, int i)
{
  int t, l, r, largest;

  if((i+1)*2+1<=h){
    l=2*(i+1)-1;
    r=2*(i+1);

    if(l<=h && A[l]>A[i]) largest=l;
    else largest=i;

    if(r<=H && A[r]>A[largest]) largest=r;
  
    if(largest!=i){
      t=*(A+i);
      *(A+i)=*(A+largest);
      *(A+largest)=t;
      maxHeapify(A, largest);
    }
  }

  else if((i+1)*2<=h){
     l=2*(i+1)-1;

    if(l<=h && A[l]>A[i]) largest=l;
    else largest=i;
  
    if(largest!=i){
      t=A[i];
      A[i]=A[largest];
      A[largest]=t;
      maxHeapify(A, largest);
    }
  }
  else{
    largest=i;
    if(largest!=i){
      t=*(A+i);
      *(A+i)=*(A+largest);
      *(A+largest)=t;
      maxHeapify(A, largest);
    }
  }
  
}

  

