#include<stdio.h>
#define MAX 500000

void maxHeapify(int);

int n,num[MAX+1];

int main() {

  int i;

  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d",&num[i]);

  for(i=n/2;i>=1;i--) maxHeapify(i);

  for(i=1;i<=n;i++) printf(" %d",num[i]);
  printf("\n");

  return 0;

}

void maxHeapify(int i) {

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