#include <stdio.h>

long int a[1000000];
int n;

void maxHeapify(int i){
  int l, r, largest, cont;
  l = 2 * i;
  r = 2 * i + 1;

  if(l<=n && a[l] > a[i])
    largest = l;
  else
    largest = i;

  if(r<=n && a[r] > a[largest])
    largest = r;
  if(largest!=i){
    //printf("cange node:%d %ld and node:%d %ld\n",i,a[i],largest,a[largest]);
    cont = a[i];
    a[i] = a[largest];
    a[largest] = cont;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int i;
  for(i=n/2;1<=i;i--){
    maxHeapify(i);
  }
}

int main(){
  int i,c;

  for(i=1;i<500;i++)a[i]='\0';

  scanf("%d",&n);

  for(i=1;i<=n;i++)
    scanf("%ld",&a[i]);
  buildMaxHeap();

  
  for(i=1;i<=n;i++)
    printf(" %ld",a[i]);

  printf("\n");
  
  
  
  return 0;
}

