#include<stdio.h>
#define NIL -2000000001

int A[1000002];
int heap_size;

int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}
void swap(int x,int y){
  int temp;
  temp=A[x];
  A[x]=A[y];
  A[y]=temp;
}

void maxHeapify(int i){
  int l = left(i);
  int r = right(i);
  int largest;
  if (l <= heap_size && A[l] > A[i])largest = l;
  else largest = i;
  if (r <= heap_size && A[r] > A[largest])largest = r;
  if (largest != i){
    swap(i,largest);
    maxHeapify(largest);
  }
}
void buildMaxHeap(){
  int i;
  for(i=heap_size/2;i!=0;i--)maxHeapify(i);
}


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

  for(i=0;i<502;i++){
    A[i]=NIL;
  }
  for(i=1;i<=heap_size;i++){
    scanf("%d",&A[i]);
  }
  buildMaxHeap();
  for(i=1;i<=heap_size;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  return 0;
}