#include <stdio.h>
#define MAX 500001

long long A[MAX];

void maxHeapify(int,int);

int main(){
  int i,n;
  
  scanf("%d",&n);

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

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

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

  printf("\n");

  return 0;
}


void maxHeapify(int i,int n){
  int large;
  int temp;

  if(2*i<=n && A[2*i]>A[i]) large=2*i;
  else large=i;
  if(2*i+1<=n && A[2*i+1]>A[large]) large=2*i+1;

  if(large!=i){
    temp=A[i];
    A[i]=A[large];
    A[large]=temp;
    maxHeapify(large,n);
  }
}