#include<stdio.h>
#define MAX 500000
int H;
void maxHeapify(int *,int);
void buildMaxHeap(int *);
int main(){
  int i;
  int Heap[MAX+1];
    scanf("%d",&H);
    for(i=1;i<=H;i++){
        scanf("%d",&Heap[i]);
    }

    buildMaxHeap(Heap);
    for(i=1;i<=H;i++){
        printf(" %d",Heap[i]);
    }
    printf("\n");
    return 0;
}

void maxHeapify(int *A,int i){
    int l,v,r,b;
    l=2*i;
    r=2*i+1;
    if(l<=H && A[l]>A[i]){
      b = l;
    }
    else b = i;
    if(r<=H && A[r]>A[b]){
    b = r;
  }
    if(b!=i){
        v=A[i];
        A[i]=A[b];
        A[b]=v;
        maxHeapify(A,b);
    }
}

void buildMaxHeap(int *A){
    int i;
    for(i=H/2;i>=1;i--){
        maxHeapify(A,i);
    }
}

