#include <stdio.h>
#define M 2000000

int N,S[M+1];
int left(int i){return i*2;}
int right(int i){return i*2 +1;}

void maxHeapify(int i){
  int l,r,lmax,swap;
  l = left(i);
  r = right(i);
  if(l <= N&&S[l]>S[i])
  {
  lmax = l;
  }
  else lmax = i;

  if(r <= N&& S[r]>S[lmax])
  {
    lmax = r;
  }
  if(lmax != i)
  {
    swap = S[i];
    S[i] = S[lmax];
    S[lmax] = swap;
    maxHeapify(lmax);
  }
}

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

return 0;
}