#include<stdio.h>
#define MAX 500000

void maxHeapify(int A[], int i,int N)
{
  int l,r,temp,largest;

  l=i*2;
  r=i*2+1;

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

  if(r<=N && A[r]>A[largest]) largest=r;

  if(largest!=i){
    temp=A[i];
    A[i]=A[largest];
    A[largest]=temp;
    maxHeapify(A, largest, N);
  }
  
}
    
int main()
{
  int i;
  int N,A[MAX];
  
  scanf("%d",&N);
  for(i=1;i<N+1;i++) scanf("%d",&A[i]);

  for(i=N/2;i>0;i--) maxHeapify(A, i, N);

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

  return 0;

}



