#include <stdio.h>
#include <stdlib.h>
#define N 500000

void maxheapify(int);

int n,arr[N+1];
int main(){
  int i,node;

  scanf("%d",&n);
  if(n>N) exit(2);
  for(i=1;i<=n;i++){
    scanf("%d",&arr[i]);
  }

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

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

void maxheapify(int node){
  int left,right,max,temp;
  left=node*2;
  right=node*2+1;
  if(left<=n && arr[left]>arr[node]) max = left;
  else max = node;
  if(right<=n && arr[right]>arr[max]) max = right;
  
  if(max != node){
    temp=arr[node];
    arr[node]=arr[max];
    arr[max]=temp;
    maxheapify(max);
  }
}

