#include <stdio.h>

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

int n,A[500000];

int main(){
  int i,j;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&A[i]);
  }
  for(i=n/2;i>=1;i--){
    maxHeapify(A,i);
  }
  for(j=1;j<=n;j++){
    printf(" %d",A[j]);
  }
   printf("\n");
  return 0;
}
void maxHeapify(int *A,int i){
  int l,r,largest,m;
  l=2*i;
  r=2*i+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){
    m=A[i];
    A[i]=A[largest];
    A[largest]=m;
    maxHeapify(A,largest);
  }
}

    
  

