#include<stdio.h>

void maxHeapify(int);

int i,n, A[500001];



int main(){

  scanf("%d",&n);

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

  for(i = n / 2 ; i >= 1 ; i--) maxHeapify(i);

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

  printf("\n");



  return 0;
}

void maxHeapify(int b){
  int ri, le, large;
  int a;
  le = 2 * b;
  ri = 2 * b+ 1;

  if(le <= n && A[le] > A [b]) large = le;
  else large = b;

  if(ri <= n && A[ri] > A[large]) large = ri;

  if(large != b){
    a = A[b];

    A[b] = A[large];

    A[large] = a;

    maxHeapify(large);
  }
}

