#include<stdio.h>
#define H 500001

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

void maxHeapify(int x){
  int largest, l,r, t;
    l=2*x;
    r=2*x+1;
    if(l<=a && A[l]>A[x])largest=l;
    else largest=x;
    if(r<=a&&A[r]>A[largest])largest=r;
    if(largest!=x){
        t=A[x];
        A[x]=A[largest];
        A[largest]=t;
        maxHeapify(largest);
    }
}

