#include<stdio.h>
#define MAX 500001

void maxHeapify(int);

int n,H[MAX];

int main(){
  int i;

  scanf("%d",&n);

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

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

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

  return 0;
}

void maxHeapify(int a){
  int l = 2*a,r = 2*a+1;
  int large,temp;

  if(l <= n && H[l] > H[a]){
    large = l;
  }
  else{
    large = a;
  }

  if(r <= n && H[r] > H[large]){
    large = r;
  }

  if(large != a){
    temp = H[a];
    H[a] = H[large];
    H[large] = temp;

    maxHeapify(large);
  }
}