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

/*
struct node{

  struct node *right;
  struct node *left;
  struct node *parent;
  int key;
};

typedef struct node *Node;
Node root, NIL;

void insert(int k){

  int N;
  Node y = NIL;
  Node x = root;
  Node z;

  z = malloc(sizeof(struct node));

  z -> key = k;
  z -> left = NIL;
  z -> right = NIL;

  while(x != NIL){

    y=x;

    if(z -> key < x -> key)
      x = x->left;
    else
      x = x -> right;
    
  }

  z -> parent = y;

  if(y == NIL) root = z;

  else {
    if( z->key < y->key)
      y -> left = z;

    else {
      y -> right = z;
    }
    
    }
}

void inorder (Node u){

  if(u==NIL) return;

  inorder(u->left);
  printf(" %d",u->key);
  inorder(u->right);

}



void preorder(Node u){
  
  if(u==NIL) return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
  
  }*/

int n,array[2000000];

void maxHeapify(int i)
{
  int l, r, largest, tmp;
  l = 2*i;//left
  r = 2*i+1;//right
  if ( l <= n && array[l] > array[i] ) largest = l;
  else largest = i;
  if ( r <= n && array[r] > array[largest] ) largest = r;
  if ( largest != i ){
    tmp = array[i];
    array[i] = array[largest];
    array[largest] = tmp;
    maxHeapify(largest);
  }

}






int main(){
  int n,i,key;
  //int array[100000];

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

  for(i=n/2; i>=1 ;i--){
    /* printf("node %d: key = %d, ",i,array[i]);
       if(i/2 >= 1) printf("parent key = %d, ",array[i/2]);
       if(i*2 <= n) printf("left key = %d, ",array[i*2]);
       if(i*2+1 <= n) printf("right key = %d, ",array[i*2+1]);
       printf("\n");*/

    maxHeapify(i);
  }

 
  for(i=1;i<=n;i++) printf(" %d",array[i]);
  printf("\n");
  
  return 0;
}

