#include <stdio.h>

void maxHeapify(int *A, int k,int n){
  int v,i,l,r,z;
  l=k*2;
  r=k*2+1;
  z=k;
  if(l<=n&&A[l]>A[k]) z=l;
  if(r<=n&&A[r]>A[z]) z=r;

  if(z!=k){
    v=A[k];
    A[k]=A[z];
    A[z]=v;
    maxHeapify(A,z,n);
  }
}

int main() {

    int i;
    int n;
    int A[500001];

    scanf("%d",&n);

    for(i=1;i<=n;i++)scanf("%d",&A[i]);

    for(i=n/2;i>=1;i--){
      maxHeapify(A,i,n);
    }

    for(i=1;i<=n;i++){
      printf(" %d",A[i]);
    }
    printf("\n");

    return 0;
}

    

