#include<stdio.h>

#define MAX 500000

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

int main(){
  int A[MAX],i,h;

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

  buildMaxHeap(A,h);

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

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

void maxHeapify(int* A,int i,int H){

  int largest,l,r,x;

  l = 2 * i;
  r = 2 * i + 1;

  if(i <= H && A[l] > A[i]) largest = l;
  else largest = i;

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

  if(largest != i){
    x = A[i];
    A[i] = A[largest];
    A[largest] = x;
    maxHeapify(A,largest,H);
  }
}