#include<stdio.h>

#define MAX 500001

int h;

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

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

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

int maxHeapify(int *A, int i){
  int l,r,largest,buf;
  l = left(i);
  r = right(i);

  if((l <= h) && (A[l] > A[i]))
    largest = l;
  else
    largest = i;
  
  if((r <= h) && (A[r] > A[largest]))
    largest = r;

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

void buildMaxHeap(int *A){
  int i;

  for(i = h / 2; i >= 1; i--)
    maxHeapify(A,i);
}

int main(){
  int i,c[MAX];

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

  buildMaxHeap(c);

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

