#include<stdio.h>

#define MAX 500000

void maxHeapify(int k);

int H,A[MAX+1];

  
int main(void){
  int i;
  
  scanf("%d",&H);

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

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

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

  puts("");

  return 0;
}


void maxHeapify(int k){
  int l,r;
  int largest;
  int temp = 0;
  
  l = 2*k;
  r = 2*k+1;

  if(l <= H && A[k] < A[l]){
    largest = l;
  } else {
    largest = k;
  }
  
  if(r <= H && A[largest] < A[r] ){
    largest = r;
  }

  if(largest != k){
    temp = A[k];        
    A[k] = A[largest] ;
    A[largest] = temp ;
    
    maxHeapify(largest);
  }
}

