#include<stdio.h>
#define MAX 500000
int n,a[MAX];
void maxHeapify(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--){
  maxHeapify(i);
 }
for(i=1;i<=n;i++){
    printf(" %d",a[i]);
 }
 printf("\n");
return 0;
}

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

