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

#define N 1000000

void maxHeapify(int);

int x,A[N];

int main() {
  int i;
  scanf("%d",&x);

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

  for(i = x / 2; i >= 1; i--) {
    maxHeapify(i);
  }

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

  return 0;
}

void maxHeapify(int a) {
  int l,r,largest,temp;
  l = 2 * a;
  r = 2 * a + 1;

  if(l <= x && A[l] > A[a]) {
    largest = l;
  }else{ 
    largest = a;
  }
  if(r <= x && A[r] > A[largest]) {
    largest = r;
  }

  if(largest != a) {
    temp = A[a];
    A[a] = A[largest];
    A[largest] = temp;
    maxHeapify(largest);
  }
}

  

