#include<stdio.h>
void buildMaxHeap();
void maxHeapify(int);
int A[1000000];
int heap;

int main(){
  int i;

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

  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 buildMaxHeap(){
    int i;
	for (i = heap / 2; i > 0; i--){
        maxHeapify(i);
	}
}

void maxHeapify(int i){
  int l, r, largest, temp;
  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){
          temp = A[i];
          A[i] = A[largest];
          A[largest] = temp;
          maxHeapify(largest);
  }
  return;
}