int right(int);
int key[H];
int i;
int main(){
  int n;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&key[i]);
}
  buildMaxHeap();
 for(i=1;i<=n;i++){
    printf(" %d",key[i]);
 }
 printf(" \n");
 return 0;
}
  void maxHeapify(int i){
    int L,R,temp;
    int largest=0;
    L=left(i);
    R=right(i);
    if(L<=H && key[L]>key[i]) largest=L;
    else largest=i;
    if(R<=H && key[R]>key[largest]) largest=R;
       if(largest!=i){
	 temp=key[i];
	 key[i]=key[largest];
	 key[largest]=temp;
	 maxHeapify(largest);
       }
  }
void buildMaxHeap(void){
  for(i=H/2;i>=1;i--){
    maxHeapify(i);
  }
}
int left(int i){
  return 2*i;
}
int right(int i){
  return 2*i+1;
}