#include <stdio.h>
#include <string.h>
#define N 2500000

int n;

void maxHeapify(int A[], int i){
  int l,r,largest,temp;
  l = left(i);
  r = right(i);
  if(l <= n && A[l] > A[i]){
    largest = l;
  }
  else 
    largest = i;
  if(r <= n && A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    int tmp;
    temp = A[largest];
    A[largest] = A[i];
    A[i] = temp;
    maxHeapify(A, largest);
  }
}

void BuildMaxHeap(int A[]){
  int i;
  for(i=n/2;i >= 1;i--){
    maxHeapify(A,i);
  }
}

int parent(int i){
  return i/2;
}

int left(int i){
  return 2*i;
}

int right(int i){
  return 2*i+1;
}

main(){
  int i,a[N];
  
  scanf("%d",&n);
  for( i = 1 ; i < n+1 ; i++ ){
    scanf("%d",&a[i]);
  }
  
  BuildMaxHeap(a);
  
  for(i=1; i<=n; i++){
    printf(" %d",a[i]);  
  }
  
  printf("\n");
  
  return 0;
}