#include<stdio.h>
#define MAX 500001
#define NIL -2000000001
int k[MAX],size;

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

int Left(int i){
  if(i*2>MAX)
    return -(1<<29);
  return i*2;
}

int Right(int i){
  if(i*2+1>MAX)
    return -(1<<29);
  return i*2+1;
}


int maxHeapify(int A[],int i){
  int l,r,largest=NIL,e;
  l=Left(i);
  r=Right(i);
  if(l<=size && A[l]>A[i])
    largest=l;
  else largest=i;
  if(r<=size && A[r]>A[largest])
    largest=r;
  if(largest!=i){
    e=A[i];
  A[i]=A[largest];
  A[largest]=e;
  maxHeapify(A, largest);
  }
    }

int buildMaxHeap(int A[]){
  int i;
  for(i=size/2;i>=1;i--){
    maxHeapify(A,i);
  }    
}

main(){
  
    int i;
  for(i=0;i<MAX;i++)
    k[i]=-(1<<29);


  scanf("%d", &size);
  for(i=1;i<=size;i++)
    scanf("%d", &k[i]);

  buildMaxHeap(k);

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

  return 0;
 
}