//Max heap

#include<stdio.h>
#define MAX 500001

int n,arr[MAX];

/*int parent(int i)
{
  i /= 2;
  return i;
}

int left(int i)
{
  i *= 2;
  return i;
}

int right(int i)
{
  i = 2 * i + 1;
  return i;
  }*/


void maxheap(int arr[],int i)
{
  int swit = 0,largest = 0;
  int l = 2 * i;
  int r = 2 * i + 1;

  if(l <= n && arr[l] > arr[i])
    {
      largest = l;
    }
  else largest = i;

  if(r <= n && arr[r] > arr[largest])
    {
      largest = r;
    }

  if(largest != i)
    {
      swit = arr[largest];
      arr[largest] = arr[i];
      arr[i] = swit;
      maxheap(arr,largest);
    }
}

void build(int arr[])
{
  for(int i = n/2; i > 0; i--)
    {
      maxheap(arr,i);
    }
}

int main()
{
  int i;

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

  build(arr);

  //printf("%d",arr[1]);
  for(i = 1; i <= n; i++)
    {
      printf(" %d",arr[i]);
    }

  printf("\n");

  return 0;
  }



