#include<stdio.h>

void maxHeapify(int *, int);
void buildMaxHeap(int *);
int H;

int main(){
  int A[500000], i;

  scanf("%d", &H);
  if(H <= 500000 && H >= 1){
    for(i = 1; i < H+1; i++){
      scanf("%d", &A[i]);
    }

    buildMaxHeap(&A[0]);

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

void buildMaxHeap(int *A){
  int i;
  for (i = H/2; i > 0; i--)
    maxHeapify(&A[0], i);
}

void maxHeapify(int *A, int i){
  int l = i * 2, r = i * 2 + 1, largest, temp;
  if(l <= H && A[l] > A[i])
    largest = l;
  else
    largest = i;

  if(r <= H && A[r] > A[largest])
    largest = r;
  if(largest != i){
    temp = A[i];
    A[i] = A[largest];
    A[largest] = temp;
    maxHeapify(&A[0], largest);
  }
}

