#include<stdio.h>
#define N 500000

typedef struct{

  int id,k,pk,lk,rk;

} Node;

int n;
Node node[N];


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

int main()
{

  int i;

  scanf("%d",&n);

  for(i=1;i<=n;i++)
    scanf("%d",&node[i].k);

  for(i=1;i<=n;i++)
    {
      if(i != 1)
	  node[i].pk = node[i/2].k;

      if(2 * i <= n)
	  node[i].lk = node[2*i].k;
      else
	node[i].lk = 0;
      
      if(2 * i + 1 <= n)
	  node[i].rk = node[2*i+1].k;
      else
	node[i].rk = 0;
    }
  i = 1;
  
  buildMaxHeap(node);
  
  for(i = 1;i <= n;i++)
    printf(" %d",node[i].k);

  printf("\n");
  
  return 0;
}


void maxHeapify(Node *A , int i)
{
  int l = 2*i, r = 2*i + 1;
  int largest,swap;

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

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

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

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

