#include<stdio.h>
#define N 500000

int b,a[N];

void maxHeapify(int i){
  int c;
  int l = 2 * i;
  int r = 2 * i + 1;
  int max=0;
  if(l <= b && a[l] > a[i]){
    max = l;
  }
  else {
    max = i;
  }

  if(r <= b && a[r] > a[max]){
    max = r;
  }

  if(max != i){
    c = a[i];
    a[i] = a[max];
    a[max] = c;
    maxHeapify(max);
  }
}

  main(){
    int i;

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