#include <stdio.h>
#include <math.h>
#define N 500

int parent(int );
int left(int );
int right(int );
void maxHeapify(int *,int );
void buildMaxHeap(int *);

int n;

main(){
  int i,*k[N],x;
  
  scanf("%d",&n);
  
  for(i=1; i<=n; i++)
    scanf("%d",(k+i));
 

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

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

  
  
int parent(int i){
  return floor(i/2);
}

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

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


void buildMaxHeap(int *k){
  int i;
  for(i=parent(n); i>0; i--)
    maxHeapify(k,i);
}


void maxHeapify(int *k, int i){

  int l,r,largest,tmp;

  l=left(i);
  r=right(i);

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

  else
    largest=i;

  if(r<=n && k[r]>k[largest])
    largest=r;
 
  if(largest!=i){
    tmp=k[i];
    k[i]=k[largest];
    k[largest]=tmp;

    maxHeapify(k,largest);
  }
}