#include<stdio.h>
#define M 500000

int H;
int T[M];

void maxHeapify(int i){
  int l,r; 
  int largest;
  int temp;

  l = i*2;
  r = i*2+1;

  if(l<=H && T[l]>T[i])
    largest = l;

  else
    largest = i;
  
  if(r<=H && T[r]>T[largest])
    largest = r;

  if(largest != i){
    temp = T[largest];
    T[largest] = T[i];
    T[i] = temp;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int i;
  for(i=H/2; i>=1; i--){
    printf("%d\n",i);
    maxHeapify(i);
  }
}
    
int main()
{
  int i;

  scanf("%d",&H);

  for(i=1; i<=H; i++){
    scanf("%d",&T[i]);
  }
  
  buildMaxHeap();
  
  for(i=1; i<=H; i++){
    printf(" %d",T[i]);
  }
  printf("\n");
  
  return 0;
}
      

