#include<stdio.h>
void maxHeapify(int);
int A[10000];
int heap;
main(){
  int i, j;
 
  scanf("%d", &heap);
  for(i = 1; i <= heap; i++){
    scanf("%d", &A[i]);
  }
  maxHeapify(1);
  for(i = 1; i <= heap; i++){
    printf(" %d", A[i]);
  }
  printf("\n");
  return 0;
}

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

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

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

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