#include <stdio.h>
#define MAX 5000000
void maxHeapify(int );
void buildMaxHeap(void); 
int par(int);
int le(int );
int ri(int );

int T[MAX],howm;

int main(){
  int i,j,p,l,r;
  scanf("%d",&howm);
  for(i=1;i<=howm;i++){
    scanf("%d",&T[i]);
  }
  buildMaxHeap();
  
  for(i=1;i<=howm;i++){
    printf(" %d",T[i]);
    /*if(i!=howm){
      printf(" ");
    }
    */
  }
    printf("\n");  
  return 0;
}


int par(int i){
  return i/2;
}

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

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

void buildMaxHeap(void){
  for(int i=howm/2;i>=1;i--){
    maxHeapify(i);
  }
}

void maxHeapify(int i){
  int l,r,largest,tr;
  l=le(i);
  r=ri(i);
  if(l<=howm && T[l]>T[i]){
    largest=l;
  }
  else{
    largest=i;
  }
  if(r<=howm && T[r]>T[largest]){
    largest=r;
  }
  if(largest!=i){
    tr=T[i];
    T[i]=T[largest];
    T[largest]=tr;
    maxHeapify(largest);
  }
}

