#include<stdio.h>

void buildMaxHeap(int *);
void maxHeapify(int*,int);

int h;

int main(){

  int A[500001],i;

  scanf("%d",&h);

  
  for(i=1;i<=h;i++){

    scanf("%d",&A[i]);

  }

  //printf("wa-i1\n");
  
  buildMaxHeap(A);

  //printf("wa-i\n");

  for(i=1;i<=h;i++){

    if(i==h)printf(" %d\n",A[i]);
    else printf(" %d",A[i]);

  }
  return 0;
}

void maxHeapify(int A[],int i){

  int l,r,largest,change;

  //printf("i:%d\n",i);
  
  l=2*i;
  r=2*i+1;

  if(l<=h && A[l]>A[i]) largest = l;
  else largest = i;

  if(r<=h && A[r] > A[largest]) largest = r;
  
  if(largest !=i){

    change = A[i];
    A[i]=A[largest];
    A[largest]=change;
    maxHeapify(A,largest);
  }


}

void buildMaxHeap(int A[]){

  int i;

  // printf("wa-i3\n");
  
  for(i=h/2;i>0;i--){
    maxHeapify(A,i);

    //printf("i:%d\n",i);
  }

  // printf("wa-i2\n");
}