#include<stdio.h>
#define MAX 2000000

int size,X[MAX];

void maxHeapify(int i){
  int l,r,LL,a;
  l = 2*i;
  r = 2*i+1;
  if(l<=size && X[l]>X[i])LL = l;
  else LL = i;
  if(r<=size && X[r]>X[LL])LL = r;
  if(LL != i){
    a = X[i];
    X[i] = X[LL];
    X[LL] = a;
    maxHeapify(LL);
  }
}
int main(){
  int i;
  scanf("%d",&size);
  for(i=1;i<=size;i++)scanf("%d",&X[i]);
  for(i=size/2;i>=1;i--)maxHeapify(i);
  for(i=1;i<=size;i++){
    printf(" %d",X[i]);
  }
  printf("\n");

  return 0;
}

