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

#define SIZE 500000

int heap[SIZE];
int n;

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

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

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

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

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

  if(r <= n && heap[r] > heap[largest]){
    largest = r;
  }

  if(largest != i){
    tmp = heap[i];
    heap[i] = heap[largest];
    heap[largest] = tmp;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  int i;
  for(i = n/2; i > 0; i--){
    maxHeapify(i);
  }
}

int main(){
  int i;

  scanf("%d",&n);

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

  buildMaxHeap();

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

  printf("\n");

  return 0;
}