#include<stdio.h>
#define MAX 500000
int A[MAX+1],N;
void maxHeapify(int);
void buildMaxHeap();
void swap(int *,int *);
int main(){
  int i;
  scanf("%d",&N);
  for(i=1;i<=N;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap();
  for(i=1;i<=N;i++) printf(" %d",A[i]);
  printf("\n");
  return 0;
}
void maxHeapify(int i){
  int l,r,largest;
  l=i*2;
  r=i*2+1;
  if(l<=N && A[l]>A[i]) largest = l;
  else largest=i;
  if(r<=N && A[r]>A[largest]) largest = r;
  if(largest != i){
    swap(&A[i],&A[largest]);
    maxHeapify(largest);
  }
}
void buildMaxHeap(){
  int i;
  for(i=N/2;i>=1;i--){
    maxHeapify(i);
  }
}
void swap(int *x,int *y){
  int z;
  z=*x;
  *x=*y;
  *y=z;
}

