#include<stdio.h>
#include<stdlib.h>

void Mheap(int *,int);

int H;

int main(){
  int i,*A;

  scanf("%d",&H);
  A=(int *)malloc(sizeof(int)*(H+1));

  for(i=1;i<=H;i++) scanf("%d",&A[i]);

  for(i=H/2;i>=1;i--) Mheap(A,i);

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

  return 0;
}

void Mheap(int *A,int i){
  int l,r,largest,swap;

  l = i*2;
  r = i*2+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){
    swap=A[largest];
    A[largest]=A[i];
    A[i]=swap;

    Mheap(A,largest);
  }
}


    
  

