#include<stdio.h>
#define MAX 1000000
int n,A[MAX+1];;

void maxHeapify(int);

int main(){
  int i;

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

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

void maxHeapify(int i){
  int left,right,big,temp;
  left=2*i;
  right=2*i+1;

  if(left<=n && A[left]>A[i])big=left;
  else big=i;
  if(right<=n && A[right]>A[big])big=right;

  if(big!=i){
    temp=A[i];
    A[i]=A[big];
    A[big]=temp;
    
    maxHeapify(big);
  }
  
}

