#include<stdio.h>
#define MAX 500000

int n;

void maxHeapify(int *A,int i)
{
  int l = 2*i;
  int r = 2*i+1;
  int largest;
  int m;
  if(l <= n && A[l] > A[i]) largest = l;
  else largest = i;
  if(r <= n && A[r] > A[largest]) largest = r;
  if(largest != i){
    m = A[i];
    A[i] = A[largest];
    A[largest] = m;
    maxHeapify(A,largest);
  }
}

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

int main()
{
  int i;
  int key[MAX+1];

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

  buildMaxHeap(key);
  
  for(i = 1;i < n+1;i++){
    printf(" %d",key[i]);
  }
  printf("\n");
  return 0;
}

