#include<stdio.h>

int H;

void maxHeapify(int *A,int i){
  int largest;
  int kari;
  if(2*i<=H&&A[2*i]>A[i])largest=2*i;
  else largest = i;
  if((2*i+1)<=H&&A[2*i+1]>A[largest])largest=2*i+1;
  if(largest!=i){
    kari=A[i];
    A[i]=A[largest];
    A[largest]=kari;
    maxHeapify(A,largest);
  }
}

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

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

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

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

  buildMaxHeap(A);

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

