#include<stdio.h>

#define MAX 500000

int Tree[MAX]; // Tree

int n;

void maxHeapify(int i){
  int largest ,exchange, gl, gr;
  gl = getLeft(i);
  gr = getRight(i);
  
  if((n >= gl) && (Tree[gl] > Tree[i])) largest = gl;

  else largest = i;
  
  if((n >= gr) && (Tree[gr] > Tree[largest])) largest = gr;

  if (largest != i){

    exchange = Tree[i];
    Tree[i] = Tree[largest];
    Tree[largest] = exchange;
    maxHeapify(largest);

  }  
}

void buildMaxHeap(){
  int i;
  for (i=(n/2); i>=1; i--){
    maxHeapify(i);
	 }
}

int getParent(int i){
  return i/2;
}

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

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

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

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

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

  }

  buildMaxHeap();

  for(i=1; i<=n; i++){
    
    printf(" %d",Tree[i]);

  }

  printf("\n");

  return 0;
}