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

void maxHeapify(int *,int,int);
void buildMaxHeap(int *,int);

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

  scanf("%d", &a);
  
  A = (int*)malloc(sizeof(int)*(a+1));
  
  for (i=0;i<a;i++) {
    scanf("%d",&A[i+1]);
  }

  buildMaxHeap(A,a);

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

  printf("\n");

  return 0;
}


void maxHeapify(int *A, int s, int t)
{
  int l,r,large, tmp;
  
  l = t * 2;
  r = l + 1;

  if (l<=s&&A[l]>A[t]) large=l;
  else large = t;

  if (r<=s&&A[r]>A[large]) large=r;

  if (large != t) {
    tmp = A[t];
    A[t] = A[large];
    A[large] = tmp;

    maxHeapify(A, s, large);
  }
}

void buildMaxHeap(int *A, int s)
{
  int i;
  
  for (i = s / 2; i > 0; i--)
    maxHeapify(A, s, i);
}

