#include <stdio.h>

int heap[500001],n;


int parent(int x)
{
  return x/2;
}

int left(int x)
{
  return x*2;
}

int right(int x)
{
  return x*2+1;
}



void maxHeapify(int *heap,int i)
{
  int l,r,largest,tmp;
  l = left(i);
  r = right(i);

  if(l <= n && heap[l] > heap[i]) largest = l;
  else largest = i;

  if(r <= n && heap[r] > heap[largest]) largest = r;

  if(largest != i){

    tmp=heap[i];
    heap[i]=heap[largest];
    heap[largest]=tmp;

    maxHeapify(heap,largest);
  }
}


void buildMaxHeap(int *heap)
{
  int j;
  for(j=n/2;j>=1;j--) maxHeapify(heap,j);
}


int main()
{
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d",&heap[i]);

  buildMaxHeap(heap);

  for(i=1;i<=n;i++) printf(" %d",heap[i]);
  printf("\n");

  return 0;
}

