#include <stdio.h>
#define H 500000

void buildMaxHeap(int *);
void maxHeapify(int *, int);
int parent(int);
int left(int);
int right(int);

int size = 0;

int main(){

  int A[H];
  int i = 0;
  
  scanf("%d",&size);
  for(i = 1; i <= size; i++){//1-origin
    scanf("%d",&A[i]);
  }

  buildMaxHeap(A);

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

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

void maxHeapify(int *A, int i){
  int l = left(i), r = right(i), largest = 0;
  int keep = 0;

  //left, right and me top of the world
  if(l <= size && A[l] > A[i]){
    largest = l;
  }
  else{
    largest = i;
  }

  if(r <= size && A[r] > A[largest]){
    largest = r;
  }

  if(largest != i){
    //A[i] koukan A[largest]
    keep = A[i];
    A[i] = A[largest];
    A[largest] = keep;
    maxHeapify(A, largest);
  }
}

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

int left(int i){
  return i*2;
}

int right(int i){
  return i*2+1;
}

