#include <stdio.h>

int parent(int);
int left(int);
int right(int);
void maxHeapify(int [], int);
void buildMaxHeap(int []);

int H, A[500001];

int main(){
  int i;

  scanf("%d", &H);

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

  buildMaxHeap(A);

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

int parent(int i){ return i/2; }

int left(int i){ return i*2; }

int right(int i){ return i*2+1; }

void buildMaxHeap(int A[]){
  int i;
  for(i = H/2; i >= 1; i--) maxHeapify(A, i);
}

void maxHeapify(int A[], int i){
  int l = left(i);
  int r = right(i);
  int large, temp;

  if(l <= H && A[l] > A[i]) large = l;
  else large = i;

  if(r <= H && A[r] > A[large]) large = r;

  if(large != i){
    temp = A[i];
    A[i] = A[large];
    A[large] = temp;
    maxHeapify(A, large);
  }
}