#include<stdio.h>
#define MAX 5000000
int size, A[MAX+1];
void maxHeapify(int i){
  int l, r, larg, t;
  l = 2*i;
  r = 2*i+1;
  if(l<=size && A[l] > A[i]) larg=l;
  else                     larg=i;
  if(r<=size && A[r]> A [larg])    larg=r;
  if(larg!=i){
    t = A[i];
    A[i] = A[larg];
    A[larg]=t;
    maxHeapify(larg);
  }
}

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

