#include<stdio.h>
#define M 500001
int h;

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

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

int main(){
  int i,heap[M];
  scanf("%d",&h);
  for(i=1;i<=h;i++)
    scanf("%d",&heap[i]);
  buildMaxHeap(heap);
  for(i=1;i<=h;i++)
    printf(" %d",heap[i]);
  printf("\n");
  return 0;
}

