#include<stdio.h>
#define MAX 2000000
int suji,A[MAX+1];
void swap(int *a,int *b);
void maxheapify(int i){
  int l,r,largest;
  l=2*i;
  r=2*i+1;

  if(l<=suji  && A[l]>A[i])largest=l;
  else largest=i;
  if(r<=suji && A[r]>A[largest])largest=r;

  if(largest!=i){
    swap(&A[i],&A[largest]);
    maxheapify(largest);
  }
}

int main()
{
  scanf("%d",&suji);
  for(int i=1;i<=suji;i++)scanf("%d",&A[i]);
  for(int i=suji/2;i>=1;i--)maxheapify(i);
  for(int i=1;i<=suji;i++){
    printf(" %d",A[i]);
  }
  printf("\n");
  return 0;
}
void swap(int *a,int *b){
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}

