#include <stdio.h>
#define MAX 500001

void maxHeapify(int);
void swap(int *, int *);
int N[MAX],H;

int main(){
  int i;

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

void maxHeapify(int n){
  int max_i;
  max_i = n;
  if(H >= (2 * n) && N[n] < N[2*n]){
    max_i = 2 * n;
  }
  if(H >= (2 * n + 1) && N[max_i] < N[2*n+1]){
    max_i = 2 * n + 1;
  }

  if(max_i != n){
    swap(&N[n],&N[max_i]);
    maxHeapify(max_i);
  }
  return;
}

void swap(int *a, int *b){
  int c;

  c = *a;
  *a = *b;
  *b = c;
  return;
}

