#include<stdio.h>
#include<stdlib.h>

void maxHeapify(int *,int,int);
void buildMaxHeap(int *,int);

int main(){

  int i,H,*key;

  scanf("%d",&H);
  key=(int *)malloc(sizeof(int)*H);
  for(i=0;i<H;i++)scanf("%d",&key[i]);

  buildMaxHeap(key,H);

  for(i=0;i<H;i++)printf(" %d",key[i]);
  printf("\n");

  free(key);

  return 0;
}
void maxHeapify(int *key,int i,int H){

  int l,r,largest,tmp;

  l=2*(i+1)-1;
  r=2*(i+1);
  if(l<= H && key[l]>key[i]){
    largest=l;
  }else largest=i;
  if(r<=H && key[r]>key[largest]) largest=r;
  if(largest!=i){
    tmp=key[i];
    key[i]=key[largest];
    key[largest]=tmp;
    maxHeapify(key,largest,H);
  }
}
void buildMaxHeap(int *key,int H){
  int i;

  for(i=H/2-1;i>=0;i--){
    maxHeapify(key,i,H);
  }
}