#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<stdbool.h>
#define lpton(i,n) for(int i=0;i<(int)n;i++)
#define lpto0(i,n) for(int i=n;i>-1;i--)
#define lpmn(i,m,n) for(int i=m;i<n;i++)
#define lpmnd(i,m,n) for(int i=m;i>n;i--)
#define ms(l,n,s) memset(l,n,s)
#define m(l,n) (l*)malloc(n*sizeof(l))
void print(int*,int);
void maxset(int*,int,int);
void structs(int*,int);
void swap(int*,int);
int main(){
  int n,v;
  int *heap;
  scanf("%d",&n);
  heap=m(int,(n+2));
  *(heap)=INT_MIN;
  *(heap+(n+1))=INT_MIN;
  lpmn(i,1,n+1){
    scanf("%d",&v);
    *(heap+i)=v;
  }
  structs(heap,n);
  print(heap,n);
  free(heap);
  return 0;
}
void print(int *heap,int n){
  lpmn(i,1,n+1){
    printf(" %d",*(heap+i));
  }
  printf("\n");
}
void maxset(int *heap,int i,int n){
  int tmp,tmpi;
  int left=2*i;
  int right=2*i+1;
  if(left>n)return;
  tmp=*(heap+left)>*(heap+right)?*(heap+left):*(heap+right);
  tmpi=*(heap+left)>*(heap+right)?left:right;
  if(tmp>*(heap+i)){
    swap(heap,tmpi);
    maxset(heap,tmpi,n);
  }
}
void swap(int *heap,int i){
int tmp;
tmp=*(heap+i);
*(heap+i)=*(heap+(i/2));
*(heap+(i/2))=tmp;
}
void structs(int *heap,int n){
  int m=n;
  lpmnd(i,m/2,0){
    maxset(heap,i,n);
  }
}

