#include <stdio.h>
#define H 2000000

int data[H],n;

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

int main(){
  int i;

  scanf("%d",&n);

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

  return 0;
}

void maxHeapify(int i){
  int l,r,largest;

    l=i*2;
    r=i*2+1;

    if(l<=n && data[l]>data[i]){
      largest=l;
    }else{
      largest=i;
    }
    if(r<=n && data[r]>data[largest]){
      largest=r;
    }

    if(largest != i){
      swap(&data[i],&data[largest]);
       maxHeapify(largest);
    }
  }
      

void swap(int *x,int *y){
  int i;
  i=*x;
  *x=*y;
  *y=i;
}
  

  

