#include<stdio.h>

#define M 500005

int n;

int l(int c){
  return c*2;
}

int r(int c){
  return c*2 + 1;
}

void maxHeapify(int *H,int i){
  int lk,rk,lar,temp;

  lk = l(i);
  rk = r(i);

  if((lk <= n) && (H[lk] > H[i])) lar = lk;
  else lar = i;
  if((rk <= n) && (H[rk] > H[lar])) lar = rk;

  if(lar != i){
    temp = H[i];
    H[i] = H[lar];
    H[lar] = temp;
    maxHeapify(H,lar);
  }
}

void buildMaxHeap(int *H){
  int i;
  
  for(i=n/2; i>0; i--) maxHeapify(H,i);
}

int main(){
  int i;
  int H[M];

  scanf("%d",&n);

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

  buildMaxHeap(H);

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

  printf("\n");
  
  return 0;
}

