#include<stdio.h>
#define MAX 500000

int size,A[MAX+1];

void swap( int  *a , int  *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
void buildmaxHeapify(int k)
{
  int left,right,big;
  left=2*k;
  right=2*k+1;

  if(left<=size && A[left]>A[k])big=left;
  else big=k;
  if(right<=size && A[right] > A[big])big=right;

  if(big!=k)
    {
      swap(&A[k],&A[big]);
      buildmaxHeapify(big);
    }
}

int main()
{
  int i;
  scanf("%d",&size);
  for(i=1;i<=size;i++)scanf("%d",&A[i]);
  for(i=size/2;i>=1;i--)buildmaxHeapify(i);
  for(i=1;i<=size;i++)printf(" %d",A[i]);
  printf("\n");

  return 0;
}