#include<stdio.h>
#define MAX 2000000

int n,tree[MAX+1];

void maxHeapify(int i){
  int l,r,large,t;
  l=2*i;
  r=2*i+1;
  if(l<=n&&tree[l]>tree[i])large=l;
  else large=i;
  if(r<=n&&tree[r]>tree[large])large=r;
  if(large!=i){
    t=tree[i];
    tree[i]=tree[large];
    tree[large]=t;
    maxHeapify(large);
  }
}

int main(){
  int i;
  scanf("%d",&n);
  for(i=1;i<=n;i++)scanf("%d",&tree[i]);
  for(i=n/2;i>=1;i--)maxHeapify(i);
  for(i=1;i<=n;i++)printf(" %d",tree[i]);
  printf("\n");

  return 0;
}

