#include<stdio.h>
#define NILL -2000000001

int H;

void maxHeapify(int *A, int i){
  
  int l,r,max,k;
  
  l = i*2;
  r = i*2+1;
  
  if((l<=H)&&(A[l] > A[i]))
    max = l;
  else max = i;

  if((r<=H)&&(A[r] > A[max]))
    max = r;

  if(max != i){
    k = A[i];
    A[i] = A[max];
    A[max] = k;

    maxHeapify(A,max);
  }

}

void buildMaxHeap(int*A){
  int i;
  
  for(i = H/2 ;i >= 1;i--)
    maxHeapify(A,i);
  
}


int main(){

  int i,k;
  int A[500001];

  scanf("%d",&H);


  for(i=1;i<=H;i++){
    scanf("%d",&k);
    
    A[i] = k;
    
  }

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

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

