#include<stdio.h>
#define N 500001
void max(int);
void build();
int left(int);
int right(int);
int A[N];
int H;
int main()
{
  int i,j;

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

  build();

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

  printf("\n");

  return 0;
}

int left(int i){
  return 2*i;
}

int right(int i){
  return 2*i+1;
}

void max(int i){
  int l,r,large,temp;
  
  l = left(i);
  r = right(i);

  if(l<=H && A[l]>A[i]) large = l;
  else large = i;

  if(r<=H && A[r]>A[large]) large = r;

  if(large!=i){
    temp = A[i];
    A[i] = A[large];
    A[large] = temp;
    max(large);
  }
}

void build(){
  int i;
  for(i=H/2; i>=1; i--){
    max(i);
  }
}