#include <stdio.h>
#define NUM 500000

struct kk {
  int key;
  int parent;
  int right;
  int left;
};


struct kk A[NUM];

void maxHeapify(struct kk A[],int i);
void buildmaxheap(struct kk A[]);

int H;

int main()
{
  int i;
  scanf("%d",&H);

  for(i = 1;i <= H;i++){
    scanf("%d",&A[i].key);
    A[i].parent = A[i/2].key;
    if(i%2 == 0) A[i/2].left = A[i].key;
    if(i%2 == 1) A[i/2].right = A[i].key;
  }

  buildmaxheap(A);

  printf(" %d",A[1].key);
  for(i = 2;i <= H;i++){
    printf(" %d",A[i].key);
  }
  printf("\n");

  return 0;
}

void maxHeapify(struct kk A[],int i)
{
  int l,r,largest,key;
  
  l = 2 * i;
  r = 2 * i + 1;

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

  if(r <= H && A[r].key > A[largest].key) largest = r;

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

void buildmaxheap(struct kk A[])
{
  int i;
  for(i = H/2;i > 0;i--) maxHeapify(A,i);
}
  

