#include<stdio.h>
#define N 500000

int H,A[N + 1];

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

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

void maxHeapify(int A[],int i){
  int l,r,t;
  int biggest = 0;
  l = left(i);
  r = right(i);
  if(l <= H && A[l] > A[i]){
    biggest = l;
  }
  else biggest = i;

  if(r <= H && A[r] > A[biggest]){
    biggest = r;
  }

  if(biggest != i){
    t = A[i];
    A[i] = A[biggest];
    A[biggest] = t;
    maxHeapify(A,biggest);
  }
}


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

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

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

  return 0;
}

