#include<stdio.h>
#define MAX 2000000

int H,st[MAX+1];

void maxHeapify(int i){
  int l,r,large,t;
  l=2*i;
  r=2*i+1;

  if(l<=H && st[l]>st[i])large=l;
  else large=i;
  if(r<=H && st[r]>st[large])large=r;

  if(large!=i){
    t=st[i];
    st[i]=st[large];
    st[large]=t;
    maxHeapify(large);
  }
}

int main(){

  int i;

  scanf("%d",&H);

  for(i=1;i<=H;i++)scanf("%d",&st[i]);

  
  for(i=(H/2);i>=1;i--)maxHeapify(i);

  for(i=1;i<=H;i++){
    if(i!=H) printf("%d ",st[i]);
    else if(i==H) printf("%d",st[i]);    
  }
  printf("\n");

  return 0;

}

