#include <stdio.h>
#define SWAP(type,a,b) { type temp = a; a = b; b = temp; }
#define H 500001

void Max_Heapify( int *A, int heap, int i){
  int l = i*2;
  int r = i*2 + 1;
  int largest;

  if(l <= heap && A[l] > A[i]){
    largest = l;
  }else{
    largest = i;
  }

  if(r <= heap && A[r] > A[largest]){
    largest = r;
  }
  if(largest != i){
    SWAP(int, A[i] , A[largest]);
    Max_Heapify(A, heap, largest);
  }
}

void buildMaxHeap(int *A, int heap){
  int i;
  for(i = heap/2; i > 0; i--){
    Max_Heapify(A, heap, i);
  }
}


int main(){

  int A[H], heap, i;

  A[0] = 0;

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

  buildMaxHeap(A, heap);

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

  return 0;
}