#include<stdio.h>

#define MAX 500000

void maxHeapify(int i, int h);

int g_A[MAX+1];

int main(){
  int h;
  scanf("%d",&h);

  for(int i = 1; i <= h; i++) scanf("%d",&g_A[i]);

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

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

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

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

  if(largest != i){
    int temp;
    temp = g_A[i];
    g_A[i] = g_A[largest];
    g_A[largest] = temp;
    maxHeapify(largest, h);
  }
}


