#include <bits/stdc++.h>
using namespace std;
void maxHeapify(vector<int32_t>& a, size_t i){
  size_t largest=i;
  if(i*2<a.size()) largest=(a[i*2]>a[largest]?i*2:largest);
  if(i*2+1<a.size()) largest=(a[i*2+1]>a[largest]?i*2+1:largest);
  if(largest!=i){
    swap(a[largest], a[i]);
    maxHeapify(a, largest);
  }
}
void buildMaxHeap(vector<int32_t>& a){
  for(size_t i=a.size()/2; i>=1; --i) maxHeapify(a, i);
}
int32_t main(){
  size_t h; cin>>h;
  vector<int32_t> a(h+1);
  for(size_t i=1; i<=h; ++i) cin>>a[i];
  buildMaxHeap(a);
  for(size_t i=1; i<=h; ++i) cout<<' '<<a[i];
  cout<<'\n';
}
