#include <stdio.h>

#define MAX 500000

void buildMaxHeap(void);
void maxHeapify(int);
int left(int);
int right(int);

int key[MAX];
int n;

int main(){
  int i;

  scanf("%d",&n);

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

  buildMaxHeap();

  for(i=1;i<=n;i++){
    if(i!=1) printf(" ");
    printf("%d",key[i]);
  }
  printf("\n");
  
  return 0;
}

void buildMaxHeap(){
  int i;

  for(i=n/2;i>0;i--){
    maxHeapify(i);
  }
}

void maxHeapify(int i){
  int l, r, k, largest;
  
  l = left(i);
  r = right(i);

  if(l<=n && key[l]>key[i]) largest = l;
  else largest = i;

  if(r<=n && key[r]>key[largest]) largest = r;

  if(largest != i){
    k = key[i];
    key[i] = key[largest];
    key[largest] = k;

    maxHeapify(largest);
  }
}

int left(int i){
  return 2*i;
}

int right(int i){
  return 2*i+1;
}