#include<stdio.h>
#include<stdlib.h>
void maxHeapify(int *,int);

int H;

int main(){
  int i;
  int *heap;
  scanf("%d",&H);
  heap=(int *)malloc(H*sizeof(int));
  for(i=1;i<=H;i++) scanf("%d",&heap[i]);
  
  for(i=H/2;i>=1;i--)maxHeapify(heap,i);

  for(i=1;i<=H;i++) printf(" %d",heap[i]);
  printf("\n");
  free(heap);
  return 0;
}

void maxHeapify(int *A,int i){
  int r,l,largest,w;
  l=2*i;
  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){
    w=A[i];
    A[i]=A[largest];
    A[largest]=w;
    maxHeapify(A,largest);
  }
  
}

