#include<stdio.h>
#define N 500002

int left(int i){return i*2;}
int right(int i){return  i*2+1;}

void maxheapify(int i, int *a, int n){
  int l,r,largest,tmp;

  l=left(i);
  r=right(i);

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

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

  if(largest!=i){
    tmp=a[i];
    a[i]=a[largest];
    a[largest]=tmp;
    maxheapify(largest,a,n);
  }
}

void print(int *a, int n){
  int i;

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

}

int main(){
  int i,a[N],n;

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

  for(i=n/2;i>0;i--) maxheapify(i,a,n);
  print(a,n);

  return 0;
}
