#include<stdio.h>

void maxHeapify(int);

int h[200000000],n;
int main(){
  int i;

  scanf("%d",&n);

  for(i=1;i<=n;i++){
    scanf("%d",&h[i]);
  }

  for(i=n/2;i>=1;i--){
    maxHeapify(i);
  }

  for(i=1;i<=n;i++){
    printf(" %d",h[i]);
  }

  printf("\n");

  return 0;
}

void maxHeapify(int i){
  int l,r,largest,temp;

  l=i*2;
  r=i*2+1;

  if(l<=n&&h[i]<h[l])largest=l;
  else largest=i;
  if(r<=n&&h[largest]<h[r])largest=r;

  if(largest!=i){
    temp=h[i];
    h[i]=h[largest];
    h[largest]=temp;
    maxHeapify(largest);
  }
}
  

