#include <stdio.h>
#include <stdlib.h>

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

int main()
{
  int H,*A,i;
  
  scanf("%d",&H);

  A = (int *)malloc((H+1) * sizeof(int));

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

  buildMaxHeap(A,H);

  for(i=1;i<H+1;i++){
    printf(" %d",A[i]);
    if(i==H) printf("\n");
  }

  free(A);
      
  return 0;
}

void buildMaxHeap(int *A,int H)
{
  int i;

  for(i=H/2;i>0;i--){
    maxHeapify(A,i,H);
  }
}

void maxHeapify(int *A,int i,int H)
{
  int l,r,largest,m;

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



