#include <stdio.h>
#define  MAX 10000000

int n;
int A[MAX+1];

void maxheapify( int A[], int i)
{
  int left;
  int right;
  int change;
  int large;
  
  left = 2*i;
  right = 2*i+1;
  
  if( left <= n && A[left] > A[i] ){
    large = left;
  }
  else{
    large = i;
  }
  
  if( right <= n && A[right] > A[large] ){
    large = right;
  }
  
  if( large != i ){
    change = A[i];
    A[i] = A[large];
    A[large] = change;
    maxheapify(A,large);
  }
}



int main()
{
  int i;

  scanf("%d",&n);
  for( i = 1; i <= n ; i++){
    scanf("%d",&A[i]);
  }
  for( i = n/2; i >= 1; i-- ){
    maxheapify(A,i);
  }
  for( i = 1 ; i <= n ; i++){
    printf(" ");
    printf("%d",A[i]);
  }
  printf("\n");
  return 0;
  
}


