#include <stdio.h>
int n,H[200000000];
void maxheap(int);

int main(){

  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&H[i]);
  }
  for(i=n/2;i>=1;i--){
    maxheap(i);
  }
  for(i=1;i<n;i++){

    printf(" %d",H[i]);
  }
    printf(" %d\n",H[i]);

    return 0;
}

  
void maxheap(int i){

  int left,right,largest,temp;
  left=i*2;
  right=i*2+1;
  if(left<=n && H[left]>H[i]){
    largest=left;
  }
  else{
    largest=i;
  }
  if(right<=n && H[right]>H[largest]){
    largest=right;
  }
  if(largest!=i){
    temp=H[i];
    H[i]=H[largest];
    H[largest]=temp;
    maxheap(largest);
  }
}
  

