#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define N 200001

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 j, r,la,temp;
  j = left(i);
  r = right(i);

  	if(j<n && arr[j] > arr[i]) {la = j;}
  	else {la = i;}
  	if(r<n && arr[r] > arr[la]) {la = r;}
  	if(la != i)
  	{
    	temp = arr[i];
    	arr[i] = arr[la];
    	arr[la] = temp;
    	maxheapify(arr,n,la);
  	}
}

int main()
{
  long long int arr[500000];
  int n;
  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("");
  
 }

