#include<stdio.h>

int heap[500010];

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

int main(){

  int i,j,sumnode;

  scanf("%d",&sumnode);

  for(i =1; i<= sumnode; i++){

    scanf("%d",&heap[i]);

  }

  buildMaxHeap(sumnode);


  for(i = 1; i <= sumnode; i++){

    printf("%d ",heap[i]);

  }

  printf("\n");

  return 0;

}

void buildMaxHeap(int sumnode){

  int i;

  for(i = sumnode/2; i >=1 ; i--){

    maxHeapify(i,sumnode);

  }

}

void maxHeapify(int i ,int sumnode){

  int left,right,largest,swap;

  left = i *2;
  right = i *2 +1;

  if(left <= sumnode && heap[left] > heap[i]){

    largest = left;

  }

  else{

    largest = i;

  }


  if(right <= sumnode && heap[right] > heap[largest]){

    largest = right;

  }


  if(largest != i){

    swap = heap[i];

    heap[i] = heap[largest];

    heap[largest] = swap;

    maxHeapify(largest,sumnode);

  }

}