#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
  int id;
  int parent;
  int left;
  int right;
}Node;
void buildMaxHeap(Node *T,int n){
  int i;
  for(i=n/2;1<=i;i--){
    maxHeapify(T, i, n);
  }
}
maxHeapify(Node *T,int i,int n){
  int l,r;
  int largest;
  Node A;
  
  l=2*i;
  r=2*i+1;
  if(l <= n && T[l].id > T[i].id){
    largest = l;
  }else largest = i;
  if(r <= n && T[r].id > T[largest].id)largest = r;
  if(largest != i){
    A = T[i];
    T[i]=T[largest];
    T[largest]=A;
    maxHeapify(T, largest,n);
  }
}
int main(){
  int n,x;
  int i;
  Node *T;
  
  scanf("%d",&n);
  T=(Node*)malloc(sizeof(Node)*(n+1));
  for(i=1;i<=n;i++){//入力
    scanf("%d",&x);
    T[i].id=x;
    T[2*i].parent=x;
    T[2*i+1].parent=x;
    if(i%2==0)T[i/2].left=x;
    else T[(i-1)/2].right=x;
    }
  buildMaxHeap(T,n);
  printf("%d",T[1]);
  for(i=2;i<=n;i++){//出力
    printf(" %d",T[i]);
    }
  printf("\n"); 
  return 0;
}

