#include <stdio.h>
#define MAX 500004

int heap[MAX], h;

void maxHeapify(int);
void buildMaxHeap();

int main(){
  int a;

  scanf("%d",&h);
  for(a = 1; a <= h; a++) scanf("%d",&heap[a]);

  buildMaxHeap();

  for(a = 1; a <= h; a++) printf(" %d",heap[a]);
  printf("\n");

  return 0;
}

void buildMaxHeap(){
  int a;
  for(a = h/2; a > 0; a--) maxHeapify(a);
}

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

  if(l <= h && heap[l] > heap[a]) largest = l;
  else largest = a;
  if(r <= h && heap[r] > heap[largest]) largest = r;
  if(largest != a){
    temp = heap[a];
    heap[a] = heap[largest];
    heap[largest] = temp;
    maxHeapify(largest);
  }
}

