#include <stdio.h>

int A[500001],n;

void maxHeapify(int i){
  int l = i*2;
  int r = i*2+1;
  int lar,tmp;
  
  if(l<=n && A[l]>A[i])lar=l;
  else lar=i;
  if(r<=n && A[r]>A[lar])lar=r;
  // printf("aaaaaaaaaaaaaa\n");
  if(i!=lar){
    tmp = A[i];
    A[i] = A[lar];
    A[lar] = tmp;
  maxHeapify(lar);
  }
}


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;
}

