#include <stdio.h>

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

int main(){
  int h,i;
  scanf("%d",&h);

  int a[h+1];

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

  buildMaxHeap(a,h);

  for(i=1;i<=h;i++){
    printf(" %d",a[i]);
  }

  printf("\n");

  return 0;
}

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

void maxHeapify(int *A, int i, int H){
  int left,right,largest,change;
    
  left=i*2;
  right=i*2+1;

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

  if(right<=H && A[right]>A[largest])
    largest=right;

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

