#include<stdio.h>
#define N 500001

//int parent(int);
int left(int);
int right(int);
void Maxheap(int *,int);
void BuildMaxheap(int *);

int H;

int main(){
  int i,A[N];

  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("\n");
  return 0;
}
/*int parent(int i){
  return i/2;
  }*/
int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
void Maxheap(int *A,int i){
  int l,r,tmp,large;
  l=left(i);
  r=right(i);
  if(l<=H && A[l]>A[i])
    large=l;
  else large=i;
  if(r<=H && A[r]>A[large])
    large=r;

  if(large!=i){
    tmp=A[i];
    A[i]=A[large];
    A[large]=tmp;
    Maxheap(A,large);
  }
}
void BuildMaxheap(int *A){
  int i;
  for(i=H/2;i>=1;i--){
    Maxheap(A,i);
  }
}