#include <stdio.h>
#define N 5000000

void maxHeapify(int);
void AB();

int H,A[N];

int main(){
  int i;
  scanf ("%d", &H);
  for (i=1; i<=H; i++) scanf("%d", &A[i]);

  AB();

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

  return 0;
}

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

void AB(){
  int i;
  for(i=H/2; i>0; --i)maxHeapify(i);
}

