#include<stdio.h>

void maxHeapipy(int*,int,int);

void maxHeapipy(int *tree,int i,int H)
{
  int left,right,max,a; /* aは交換用 */

  left = i*2;
  right = i*2+1;

  if(left <= H && tree[left] > tree[i]) {
    max =left;
  }else{
    max = i;
  }

  if(right <=H && tree[right] > tree[max])
    max = right;

  if(max != i) {
    a = tree[i];
    tree[i] = tree[max];
    tree[max] = a;
    maxHeapipy(tree,max,H);
  }
}

int main()
{
  int H,i;
  int tree[500000];

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

  for(i=H/2;i>=1;i--) 
    maxHeapipy(tree,i,H);

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

  printf("\n");

  return 0;
}

