#include<stdio.h>

#define MAX 2000000

int size,a[MAX+1];

void maxHeapify(int i){
  int l,r,large,t;

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

  if((l <= size) && (a[l] > a[i])){
    large = l;
  }

  else{
    large = i;
  }
  
  if((r <= size) && (a[r] > a[large])){
    large = r;
  }
  
  if(large != i){
    t = a[i];
    a[i] = a[large];
    a[large] = t;
    
    maxHeapify(large);

  }
}

int main(){

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

  for(i=size/2;i>=1;i--){
    maxHeapify(i);
  }

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

  
  printf("\n");

  return 0;

}

