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

int left(int i){
  return 2*i;
}

int right(int i){
  return 2*i+1;
}

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

  l=left(i);
  r=right(i);
  if(l<=H && A[l]>A[i])
    largest=l;
  else largest=i;
  
  if(r<=H && A[r]>A[largest])
    largest=r;
  if(largest!=i){
    ex=A[largest];
    A[largest]=A[i];
    A[i]=ex;
    maxHeapify(A,H,largest);
  }
}

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

void print(int *A,int H){
  int i;
  for(i=1;i<=H;i++)
    printf(" %d",A[i]);
}

int main(){
  int H,i,*A;
  scanf("%d",&H);
  A=malloc(sizeof(int*)*(H+1));
  for(i=1;i<=H;i++)
    scanf("%d",&A[i]);
  buildMaxHeap(A,H);
  print(A,H);
  printf("\n");
  return 0;
}

