#include<stdio.h>
#define N 500000

int n,x[N];
void maxHeapify(int i){
  int l,r,largest,swap;
  l=2*i;
  r=2*i+1;
  if(l<=n && x[l]>x[i]){
    largest=l;
  }
  else largest=i;
  if(r<=n && x[r]>x[largest]){
    largest=r;
  }
  if(largest!=i){
    swap=x[i];
    x[i]=x[largest];
    x[largest]=swap;
    maxHeapify(largest);
      }
}

int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&x[i]);
  }
  for(i=n/2;i>=1;i--){
    maxHeapify(i);
  }
  for(i=1;i<=n;i++){
    printf(" %d",x[i]);
  }
  printf("\n");

  return 0;
}
