#include <stdio.h>
int heap[500000];
int n;
 
void getmax(int);
void build(void);

int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++) scanf("%d",&heap[i]);
  build();
  for(i=1;i<=n;i++) printf(" %d",heap[i]);
  printf("\n");
  return 0;
 }

void getmax(int i){
  int l=2*i,r=2*i+1,a,p;
  if(l<=n && heap[i]< heap[l]) a=l;
    else a=i;
    if(heap[r] > heap[a] && r<=n) a=r;
    if(a!=i){
      p=heap[i];
      heap[i]=heap[a];
      heap[a]=p;
      getmax(a);
    }
}

void build(void){
  int i;
  for(i=(n/2);i>=1;i--) getmax(i);
}