#include <stdio.h>

int H[500001],n;
void maxHeapify(int);
int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&H[i]);
  }

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

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

  if(largest != i){
    hand = H[i];
    H[i] = H[largest];
    H[largest] = hand;
    
    maxHeapify(largest);
  }
}

