#include<stdio.h>
#include<stdlib.h>

typedef struct{
  int k;
  int pk;
  int lk;
  int rk;
  int h;
  int lh;
  int rh;
}Node;

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

int heap;

int main(){
  int i,n;
  Node *node;

  scanf("%d",&n);

  heap = n;
  
  node = (Node *)malloc(sizeof(Node) * n+1);

  
  for(i = 1 ; i < n+1 ; i++){
    scanf("%d",&node[i].k);
    node[i].h = i;
  }

  for(i = 0 ;i <= 0 ; i++){
    node[i].k = 0;
    node[i].pk = 0;
    node[i].lk = 0;
    node[i].rk = 0;
    node[i].rk = 0;
    node[i].h = 0;
    node[i].lh = 0;
    node[i].rh = 0;
  }
  for(i = 1 ; i < n+1 ; i++){
      
    node[i].pk = node[i/2].k;
    node[i].lk = node[2 * i].k;
    node[i].rk = node[2 * i + 1].k;
    node[i].lh = 2 * i;
    node[i].rh = 2 * i + 1;
    
      
  }


  buildMaxHeap(node);
  
  for(i = 1 ; i < n+1 ; i++) printf(" %d",node[i].k);

  printf("\n");


  return 0;
}


void buildMaxHeap(Node *node){
  int i;
  for(i = heap/2 ; i >= 1 ; i--){
    printf("a\n");
    maxHeapify(node,i);
  }
}

void maxHeapify(Node *node,int i){
  int l,r,largest,s;

  l = node[i].lh;
  r = node[i].rh;

  if(l <= heap  && node[l].k > node[i].k)
    largest = l;

  else
    largest = i;

  if(r <= heap  && node[r].k > node[largest].k)
    largest = r;


  if(largest != i){
    s = node[i].k;
    node[i].k = node[largest].k;
    node[largest].k = s;
    maxHeapify(node,largest);
  }
}
    