#include<stdio.h>

#define MAX 500001

int heap;

void maxHeapify(int *, int);
void buildMaxHeap(int *);

int main(){
  int A[MAX];
  int i;

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

  buildMaxHeap(A);

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

void maxHeapify(int *A, int i){
  int left,right,largest,tmp;

  left=2*i;
  right=left+1;

  if(left<=heap && A[left]>A[i]) largest=left;
  else largest=i;
  if(right<=heap && A[right]>A[largest]) largest=right;

  if(largest!=i){
    tmp=A[largest];
    A[largest]=A[i];
    A[i]=tmp;
    maxHeapify(A,largest);
  }
}

void buildMaxHeap(int *A){
  int i;

  for(i=heap/2;1<=i;i--) maxHeapify(A,i);
}

