/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   main.c
 * Author: kundaikwangwari
 *
 * Created on July 16, 2019, 2:19 PM
 */

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

int HeapSize;

void swap(int *A, int i, int j){
    
    int temp = A[i];
    A[i] = A[j];
    A[j] = temp;
}

void MaxHeapify(int *A, int i){
    
    int LF, RL, PN, largest;
    LF = 2*i;
    RL = 2*i + 1;
      // select the node which has the maximum value
      if((LF <= HeapSize) && (A[LF] > A[i]))largest = LF;
      else largest = i;
      if((RL <= HeapSize) && (A[RL] > A[largest]))largest = RL;
    
     if(largest != i){
         // value of children is larger than that of i
         swap (A,i,largest);
         MaxHeapify(A,largest);
     }
    

 
}


int main(int argc, char** argv) {

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

    for(int i = (n+1/2)+1; i >= 1 ;i--){
        MaxHeapify(A,i);
    }
    
    for(int i = 1; i < n + 1; i++){
        
      printf(" %d",A[i]);  
        
    }
    printf("\n");
    return (0);
}


