#include <stdio.h>

#define M 500002

#define parent(i) (i/2)
#define left(i) (i*2)
#define right(i) (i*2+1)

void maxHeapify(int *,int);
void buildMaxHeap(int *);

int num;

int main(){
  int i;
  int A[M];

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

  buildMaxHeap(A);

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

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

void maxHeapify(int *A,int i){
  int l,r,large,temp;

  l=left(i);
  r=right(i);

  if(l<=num && A[l]>A[i]) large=l;
  else large=i;
  if(r<=num && A[r] > A[large]) large=r;
  if(large!=i){
    temp=A[i];
    A[i]=A[large];
    A[large]=temp;
    maxHeapify(A,large);
  }
}

