#include<stdio.h>

#define N 500000

int MaxH[N];

void MaxHeap(int,int );


int main(){
  int loop;
  int i;

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

  for(i=(loop/2);i>=1;i--){
    MaxHeap(i,loop);
  }

  for(i=1;i<loop+1;i++){
    printf(" %d",MaxH[i]);
  }

  printf("\n");
  
  return 0;
}

void MaxHeap(int Heapn,int loop){
  int left,right,temp;
  int Bscale;

  
  left=2*Heapn;
  right=(2*Heapn)+1;

  if((loop>=left) && (MaxH[Heapn]<MaxH[left])){
    Bscale=left;
  }

  else{
    Bscale=Heapn;
  }

  if((loop>=right) && (MaxH[Bscale]<MaxH[right])){
    Bscale=right;
  }

  if(Heapn!=Bscale){
    temp=MaxH[Heapn];
    MaxH[Heapn]=MaxH[Bscale];
    MaxH[Bscale]=temp;

    MaxHeap(Bscale,loop);
  }
     
}

