#include<stdio.h>
#define N 600000

typedef struct{
  int key;
  int par;
  int left;
  int right;
}node;

//void ndset(int,int); 
void maxHeapify(int);

node T[N];

int main(){
  int n,i;

  scanf("%d",&n);

  //same as 9-1.c 
  for(i=0; i<n; i++) T[i].par = -1;

  for(i=0; i<n; i++) scanf("%d",&T[i].key);

  //same as 9-1.c order 
  for(i=0; i<n; i++){
    
    if(i != 0) T[i].par = (i+1)/2-1;

    if(2*(i+1)-1 < n) T[i].left = 2*(i+1)-1;
    else T[i].left = -1;

    if(2*(i+1) < n) T[i].right = 2*(i+1);
    else T[i].right = -1;

  }

  for(i=n/2; i>=0; i--) maxHeapify(i);
  
  for(i=0;i<n;i++) printf(" %d",T[i].key);
  printf("\n");

  return 0;
}


void maxHeapify(int i){

  int l,r,big,temp;

  l = T[i].left;
  r = T[i].right;
  
  if(l != -1 && T[l].key > T[i].key) big = l;
  else big = i; //biggest is the i.
  
  if(r != -1 && T[r].key > T[big].key) big = r;
  
  if(big != i){
    temp = T[big].key;
    T[big].key = T[i].key;
    T[i].key = temp;
  
    maxHeapify(big);

  }
}