#include<stdio.h>
#define M 2000000
int n,A[M];

void maxH(int);
void swap(int *,int *);


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

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

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

void maxH(int i){
  int l,r,b;
  l=2*i;
  r=2*i+1;
    if(l<=n&&A[l] >A[i]) b =l;
    else b = i;

    if(r<=n&&A[r]>A[b]) b =r;
  if(b!=i){
    swap(&A[i],&A[b]);
    maxH(b);
    
  }
}

void swap(int *a,int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
  return;
}


