#include<stdio.h>
#define N 500000

int H;
int left(int);
int right(int);
void maxHeapify(int *,int);
void buildMaxHeap(int *);

int main(){
  int i;
  int a[N];

  scanf("%d",&H);

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

  buildMaxHeap(a);

  for(i=1; i<=H; i++){
    printf(" %d",a[i]);
  }

  printf("\n");

  return 0;
}

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

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

void maxHeapify(int *a,int i){
  int l,r,largest,temp;

  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){
    temp = a[i];
    a[i] = a[largest];
    a[largest] = temp;
    maxHeapify(a,largest);
  }
}

void buildMaxHeap(int *a){
  int i;

  for(i=H/2; i>0; i--) maxHeapify(a,i);
}

