#include<stdio.h>

#define N 2000000

int H,n[N];
/*typedef struct node {
  int pa;
  int left;
  int right;
  int key;
} Tree;
*/
int pa(int x) {return x/2;}
int le(int x) {return x*2;}
int ri(int x) {return x*2+1;}

void maxHe(int x)
{
  int swp,l,r,big;
  l=le(x);
  r=ri(x);
  if(l <= H && n[l] > n[x]) big=l;
  else big=x;
  if(r <= H && n[r] > n[big]) big=r;

  if(big != x)
    {
      swp=n[x];
      n[x]=n[big];
      n[big]=swp;
      maxHe(big);
    }
}
  


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

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

  for(i=H/2; i>=1; i--)
    {
      maxHe(i);
    }

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

  return 0;
}

