#include<stdio.h>
#include<stdlib.h>
#define MAX 500001

int size;

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

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

void maxHeapify(int A[],int id){
  int l = Left(id);
  int r = Right(id);
  int largest,temp;

  if(l<=size && A[l]>A[id]) largest = l;
  else largest = id;

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

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

void builtMaxHeap(int A[]){
  int i;
  for(i=size/2 ; i>0 ; i--){
    maxHeapify(A,i);
  }
}

main(){
  int heap[MAX],id;
  
  scanf("%d",&size);
  for(id=1 ; id<=size ; id++){
    scanf("%d",&heap[id]);
  }

  builtMaxHeap(heap);

  for(id=1 ; id<=size ; id++){
    printf(" %d",heap[id]);
  }
  printf("\n");

  return 0;
}