#include<stdio.h>
#define N 500001

int a[N],h;

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


void ex(int i,int j)
{
  int t;
  t=a[i];
  a[i]=a[j];
  a[j]=t;
}

void max(int i){
  int l,r,largest;
  l=left(i);
  r=right(i);
  if(l<=h&&a[l]>a[i])
    largest=l;
  else
    largest=i;
  if(r<=h&&a[r]>a[largest])
    largest=r;

  if (largest!=i){
    ex(i,largest);
    max(largest);
  }
  
}


void buildMaxHeap(){
  int i;
  for(i=h/2;i>=1;i--)
    max(i);
}




int main(){
 int i;

  scanf("%d",&h);
  for(i=1;i<=h;i++){
    scanf("%d",&a[i]);
  }
  buildMaxHeap();

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

  
  return 0;
}

