#include<stdio.h>

int A[500001];

void maxheap(int H,int i)
{
  int l,r,max;

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

  if (l <= H && A[l] > A[i]) max = l;
  else max = i;

  if(r <= H && A[r] > A[max]) max = r;

  if(max != i)
  {
    l = A[i];
    A[i] = A[max];
    A[max] = l;
    maxheap(H,max);
  }
}

void build(int H)
{
  int i;
  for(i = H/2;i > 0;i--) maxheap(H,i);
}

int main()
{
  int n,i;

  scanf("%d",&n);

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

  build(n);

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

  printf("\n");

  return 0;
}

