#include <stdio.h>
#define MAX 5000000
int H[MAX],n;

void maxHeapify(int h){
  int r,l,big,temp;
  r = (h*2)+1;
  l = h*2;
  
  if(H[l] > H[h] && l <= n)
    big = l;
  else
    big = h;
  if(n >= r && H[r] > H[big])
    big = r;
  
  if(big != h){
    temp = H[h];
    H[h] = H[big];
    H[big] = temp;
    maxHeapify(big);
  }
}

void buildMaxHeap(void){
  int i;
  for(i=n/2;i>=1;i--) {
    maxHeapify(i);
  }
}
    
int main(){
  int i;
  
  scanf("%d",&n);

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

  buildMaxHeap();

  for(i=1;i<=n;i++)
    printf(" %d",H[i]);
  printf("\n");

  return 0;
}

