#include <stdio.h>

int n,A[2500000];

void maHe(int);

int main(){
  int i,a;

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

  a=n/2;
  for(i=a;i>0;i--){
    maHe(i);
  }
  for(i=1;i<=n;i++){
    printf(" %d",A[i]);
  }
  printf("\n");

  return 0;
    
}

void maHe(int i){
  int l,r,la,tp;

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

  if(l<=n && A[l]>A[i])la=l;
  else la =i;
  if(r<=n && A[r]>A[la])la=r;
  if(la!=i){
    tp=A[i];
    A[i]=A[la];
    A[la]=tp;
    maHe(la);
  }
}

