#include <stdio.h>

#define SWAP(x, y) ((x) += (y), (y) = (x) - (y), (x) -= (y))
#define LEFT(x) (2 * x)
#define RIGHT(x) (2 * x + 1)

int heap_size;

void maxHeapify(int *a, int i){
  int l = LEFT(i), r = RIGHT(i), largest;
  if(l <= heap_size && a[l] > a[i]) largest = l;
  else largest = i;
  if(r <= heap_size && a[r] > a[largest]) largest = r;
  if(largest != i){
    SWAP(a[i], a[largest]);
    maxHeapify(a, largest);
  }
  return;
}

int main(){
  int a[500005], i;

  scanf("%d", &heap_size);
  for(i = 1; i <= heap_size; ++i) scanf("%d", a + i);

  for(i = heap_size / 2; i > 0; --i) maxHeapify(a, i);

  for(i = 1; i <= heap_size; ++i) printf(i != 1 ? " %d" : "%d", *(a + i));
  puts("");
  return 0;
}