#include<stdio.h>

void maxheapify(int);

int n,a[2000001];

int main(){
  int i;

  scanf("%d",&n);

  for(i=1;i<=n;i++){
    scanf("%d",&a[i]);
  }
  
  for(i=n/2;i>=1;i--) maxheapify(i);
  
  for(i=1;i<=n;i++){
    printf(" %d",a[i]);
  }

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

void maxheapify(int x){
  int r,l,largest;
  int tmp;
  l=2*x;
  r=2*x+1;

  if(l<=n&&a[l]>a[x]) largest=l;
  else largest=x;

  if(r<=n&&a[r]>a[largest]) largest=r;

  if(largest!=x){
    tmp=a[x];
    a[x]=a[largest];
    a[largest]=tmp;

    maxheapify(largest);
  }
}

