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

#define MAX_H 500000

void maxHeapify (int *,int);
void buildMaxHeap (int *);
int left (int i){ return i * 2; }
int right (int i){ return i * 2 + 1; }
int h;

int main (void)
{
  int *a,i;

  if (h <= MAX_H)
    scanf ("%d",&h);

  a = malloc(sizeof(int) * h);

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

  buildMaxHeap(a);

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

  return 0;
}

void maxHeapify (int *a,int i)
{
  int l,r,largest,x;

  l = left(i);
  r = right(i);

  //左の子、自分、右の子で値が最大のnodeを選ぶ
  if (l <= h && a[l] > a[i])
    largest = l;
  else
    largest = i;
  if (r <= h && a[r] > a[largest])
    largest = r;

  if (largest != i){
    //exchange
    x = a[i];
    a[i] = a[largest];
    a[largest] = x;

    maxHeapify(a,largest);
  }

  return;
}

void buildMaxHeap (int *a)
{
  int i;

  for (i = h / 2 ; i >= 1 ; i--)
    maxHeapify(a,i);

  return;
}