#include<stdio.h>
#define H 500000

int heaps[H+1];
int n;

int parent(int i){
  return 1/2;
}
int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
void MaxHeapify(int i){
  int l,r,larg,tmp;

  l=left(i);
  r=right(i);

  if(l<=n&&heaps[l]>heaps[i]){
    larg=l;
  }else{
    larg=i;
  }

  if(larg!=i){
    tmp=heaps[i];
    heaps[i]=heaps[larg];
    heaps[larg]=tmp;
    MaxHeapify(larg);
  }
}
void buildMaxHeap(){
  int i;
  for(i=n/2;i>0;i--){
    MaxHeapify(i);
  }
}

int main(){
  int i,x;

  scanf("%d",&n);

  for(i=0;i<n;i++){
    scanf("%d",&heaps[i]);
  }
  buildMaxHeap();
  for(i=0;i<n;i++){
    printf(" %d",heaps[i]);
  }
  printf("\n");

  return 0;
}