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