#include <stdio.h>
#include <stdlib.h>

void maxHeapify(int*,int);
void buildMaxHeap(int*);

int H;

int main(){
  int *A;
  int i;
  
  //input
  scanf("%d",&H);
  A = malloc(sizeof(int)*(H+1));
  for(i = 1; i < H+1; i++) scanf("%d",&A[i]);

  //process
  buildMaxHeap(A);

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

void maxHeapify(int* A, int i){
  int l, r, largest, temp;
  l = i*2;
  r = i*2+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){
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeapify(A, largest);
  }// 再帰的に呼び出し
}

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

