#include <stdio.h>

#define NIL -2000000001

typedef struct{
  int id;
  int l;
  int r;
} Node;

int L,R,n,m,i,largest;
Node node[500001];

void maxHeapify(Node A, int a){
  if(2*a <= n && node[A.l].id > node[a].id){
    largest = A.l;
  } else {
    largest = a;
  }
  if(2*a+1 <= n && node[A.r].id > node[largest].id){
    largest = A.r;
  }
  if(largest != a){
    m = node[a].id;
    node[a].id = node[largest].id;
    if(node[a].l == largest){
      node[2*a].id = m;
      maxHeapify(node[2*a],2*a);
    } else {
      node[2*a+1].id = m;
      maxHeapify(node[2*a+1],2*a+1);
    }
  }
}

void buildMaxHeap(void){
  for(i=n/2; i >= 1; i--){
    maxHeapify(node[i],i);
  }
}

int main(){

  scanf("%d",&n);

  for(i=1; i <= n; i++){
    node[i].l = node[i].r = NIL;
    scanf("%d",&node[i].id);
  }

  for(i=1; i <= n; i++){
    if(2*i <= n){
      node[i].l = 2*i;
    }
    if(2*i+1 <= n){
      node[i].r = 2*i+1;
    }
  }
  
  buildMaxHeap();
  
  for(i=1; i <= n; i++){
    printf(" %d",node[i].id);
  }
  printf("\n");

  return 0;

}

