#include <stdio.h>
#define SIZE 2000000

void maxHeapify(int i);
  
int n;
int hi[SIZE];

int main(){
  int i;

  scanf("%d",&n);

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

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

  return 0;
}

void maxHeapify(int i){
  int l,r,max,tem;
  l=2*i;
  r=2*i+1;
  
  if(l<=n && hi[l]>hi[i]){
    max=l;
  }
  else max=i;
  if(r<=n && hi[r]>hi[max]){
    max=r;
  }

  
  if(max != i){
    tem =hi[i]; 
    hi[i]=hi[max];
    hi[max]=tem;
    maxHeapify(max);
  }
}

