#include<stdio.h>

int H[500001],size;

int left(int l){
  return 2*l;
}

int right(int r){
  return 2*r+1;
}

void maxheapify(int h[],int i){
  int largest,ln,rn,an;
  ln=left(i);
  rn=right(i);

  if(ln<=size && h[ln]>h[i]){
    largest=ln;
  }

  else largest=i;

  if(rn<=size && h[rn]>h[largest]){
    largest=rn;
  }

  if(largest!=i){
    an=h[largest];
    h[largest]=h[i];
    h[i]=an;
    maxheapify(h,largest);
  }
}

void Buildmaxheap(int h[]){
  int i;
  for(i=size/2;i>=1;i--){
    maxheapify(h,i);
  }
}

int main(){
  int i;

  scanf("%d",&size);
  for(i=1;i<=size;i++){
    scanf("%d",&H[i]);
  }

  Buildmaxheap(H);

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

  return 0;
}