#include <stdio.h>
#include <stdlib.h>

#define HMAX 500000
#define MAX 2000000000

void maxHeapify(int, int *, int);

int main() {
  int A[HMAX], H, i;

  scanf("%d", &H);
  if(H<1||HMAX<H) exit(8);
  for(i=1; i<=H; i++) {
    scanf("%d", &A[i]);
    if(A[i]<(-1)*MAX||MAX<A[i]) exit(8);
  }
  
  for(i=H/2; i>=1; i--)
    maxHeapify(H,A,i);

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

void maxHeapify(int H, int *A, int i) {
  int l, r, largest=i, tmp;

  l = 2*i;
  r = 2*i+1;
  
  if(l<=H&&A[largest]<A[l]) largest = l;
  if(r<=H&&A[largest]<A[r]) largest = r;
  if(largest!=i) {
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(H,A,largest);
  }
}

