#include <stdio.h>
#include <stdlib.h>

/*
typedef struct{
  int id;
  int parent;
  int left;
  int right;
}Tree;
*/
int *A;
int n;

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

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

  if((l <= n) && (A[l] > A[i])){
    largest = l;
  }else{
    largest = i;
  }
  if ((r <= n) && (A[r] > A[largest])) {
    largest = r;
  }
  printf("largest:%d\n",largest );
  printf("A[largest]:%d\n",A[largest]);
  if(largest != i){
    printf("A[%d]:%d\n",i,A[i] );
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(largest);
  }
}

void buildHeap(){
  int i;

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

void printer(){
  int i;

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

int main() {
  int i;

  scanf("%d",&n );
  A = (int *)malloc(sizeof(int) * (n+1));

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

  buildHeap();
  printer();

  free(A);
  return 0;
}

