#include <stdio.h>
#include <stdlib.h>

#define H 500001

void maxHeapify(int [], int, int);
void buildMaxHeap(int [], int);

int main(){
  int i;
  int h;
  int a[H];

  scanf("%d",&h);
  if(h<1 || h>H) exit(1);

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

  buildMaxHeap(a, h);

  for(i=1; i<h+1; i++){
    printf(" %d", a[i]);
    if(i==h) printf("\n");
  }

  return 0;
  
}

void buildMaxHeap(int a[], int h){
  int i;
  for(i=h/2; i>0; i--) maxHeapify(a, i, h);
  return;
}

void maxHeapify(int a[], int i, int h){
  int left, right, big, tmp;

  left = i*2;
  right = left+1;

  if(left <= h && a[left]>a[i]) big = left;
  else big = i;

  if(right <= h && a[right] > a[big]) big = right;

  if(big != i){
    tmp = a[big];
    a[big] = a[i];
    a[i] = tmp;
    maxHeapify(a, big, h);
  }
  return;
}

