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

#define NIL -2000000000

typedef struct{
  int k;
  int lk;
  int rk;
}Node;

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

int main(){

  Node *A;
  int h,i,x;

  scanf("%d",&h);

  A = malloc((h+1)*sizeof(Node));

  for(i = 1 ; i <= h ; i++) A[i].k = A[i].lk = A[i].rk = NIL;


  for(i = 1 ; i <= h ; i++){
    scanf("%d",&x);

    A[i].k = x;
    if(i%2 == 0) A[i/2].lk = x;
    else if(i%2 == 1) A[i/2].rk =x;

  }

  buildMaxHeap(A, h);

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

    printf(" %d",A[i].k);

  }

  printf("\n");

  return 0;

}


void buildMaxHeap(Node *A, int h){
  int i;
  for(i = h/2 ; i >= 1 ; i--)
    maxHeapify(A, i, h);
}

void maxHeapify(Node *A, int i, int h){
  int l, r, largest,tmp;

  l = i*2;
  r = i*2+1;

  if(l <= h && A[l].k > A[i].k) largest = l;
  else largest = i;

  if(r <= h && A[r].k > A[largest].k) largest = r;


  if(largest != i){
    tmp = A[i].k;
    A[i].k = A[largest].k;
    A[largest].k = tmp;
    maxHeapify(A, largest, h);
  }

}