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

long long int parent(long long int i)
{
  return (i-1)/2;
}
long long int left(long long int i)
{
  return 2*i+1;
}
long long int right(long long int i)
{
  return 2*i+2;
}

void maxheapify(long long int arr[], int n, int i)
{
  int l, r,largest,temp;
  l = left(i);
  r = right(i);

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

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

  	if(largest != i)
  	{
    	temp = arr[i];
    	arr[i] = arr[largest];
    	arr[largest] = temp;
    	maxheapify(arr,n,largest);
  	}
}

int main()
{
  int n;
  long long int arr[500000];
  scanf("%d", &n);

  for(int i=0; i<n; i++)
    scanf("%lld", &arr[i]);

  for(int i=(n-1)/2; i>=0; i--)
    maxheapify(arr,n,i);

  for(int i=0;i<n;i++)
    printf(" %lld",arr[i]);
	puts("");
 }
