#include<stdio.h>

#define MAX 2000000

void maxHeapify(int);

int n,arr[MAX+1];

int main(){

  int i;

  scanf("%d",&n);

  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 i){

  int l,r,L,tmp;

  l=2*i;
  r=2*i+1;

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

  if(r<=n&&arr[r]>arr[L]){
    L=r;
  }

  if(L!=i){
    tmp = arr[i];
    arr[i] = arr[L];
    arr[L] = tmp;
    maxHeapify(L);
  }
}