#include <stdio.h>
#define N 2000000

int h;
int A[N+1];

void swap(int *a,int *b){
  int tmp;
  tmp=*a;
  *a=*b;
  *b=tmp;
}
void maxHeapfy(int i){
  int largest,l=2*i,r=2*i+1;

  if(l<=h&&A[l]>A[i]) largest=l;
  else largest=i;
  if(r<=h&&A[r]>A[largest]) largest = r;
  if(largest!=i){
    swap(&A[i],&A[largest]);
    maxHeapfy(largest);
  }
}

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


int main(){
  int i;
  scanf("%d",&h);
  for(i=1;i<=h;i++) scanf("%d",&A[i]);
  buildMaxHeap();

  for(i=1;i<=h;i++) printf(" %d",A[i]);
  printf("\n");
  return 0;
}