#include <stdio.h>
#define MAX 500000

int A[MAX],H;
void maxHeapify(int);

int main()
{
  int i;
  scanf("%d",&H);
  for(i=1;i<=H;i++)
    {
      scanf("%d",&A[i]);
    }
  for(i=H/2;i>=1;i--)
    {
      maxHeapify(i);
    }
  for(i=1;i<=H;i++)
    {
      printf(" %d",A[i]);
    }
  printf("\n");
  return 0;
}

void maxHeapify(int i)
{
  int l,r,largest,change=0;
  l = 2*i;
  r = 2*i+1;
  // 左の子、自分、右の子で値が最大のノードを選ぶ
  if ((l <= H)&&(A[l] > A[i]))
    {
      largest = l;
    }
  else
    {
      largest = i;
    }
  if ((r <= H )&& (A[r] > A[largest]))
    {
      largest = r;
    }

  if (largest != i)// i の子の方が値が大きい場合
    {
      //A[i] と A[largest] を交換
      change=A[i];
      A[i]=A[largest];
      A[largest]=change;
      maxHeapify(largest); // 再帰的に呼び出し
    }
}

/*
#include <stdio.h>
#define MAX 2000000

int h,a[MAX+1];

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

  if(l<=h && a[l] >a[i])largest=l;
  else largest = i;
  if(r <=h && a[r] > a[largest]) largest = r;

  if(largest != i){
    oki = a[i];
    a[i] = a[largest];
    a[largest] = oki;
    maxHeapify(largest);
  }
}
int main()
{
  int i;
  scanf("%d",&h);

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

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

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

  return 0;
}


void buildMaxHeap(A)
{
  for (i=H/2;i>=1;i--)
    {
      maxHeapify(A, i);
    }
}

 */

