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

int n;
int *A;

void maxHeapify(int);
void build();
void swap(int *, int *);

int main(){
  int i;

  scanf("%d",&n);

  A = (int *)malloc((n+1) * sizeof(int));

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

  build();

  for(i=1; i<=n; i++)
    printf(" %d",A[i]);

  printf("\n");

  free(A);
  return 0;
}

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

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

  else
    largest = i;

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

  if(largest != i){
    swap(&A[i], &A[largest]);
    maxHeapify(largest);
  }
}

void build(){
  int i;

  for(i=n/2; i>0; i--)
    maxHeapify(i);
}

void swap(int *a, int *b){
  int tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;
}

