#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cstring>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

using namespace std;

const int N_MAX = 2000000001;
int a[N_MAX];

void maxheapfy(int ix, int n){
  int root_key = a[ix];
  int l_key = -N_MAX;
  int r_key = -N_MAX;
  if(2*ix+1 < n) l_key = a[2*ix+1];
  if(2*ix+2 < n) r_key = a[2*ix+2];
  // r max
  if(root_key < r_key && l_key < r_key){
    // swap r and root
    swap(a[2*ix+2], a[ix]);
    maxheapfy(2*ix+2, n);
  }
  else if(root_key < l_key && r_key < l_key){
    // swap l and root
    swap(a[2*ix+1], a[ix]);
    maxheapfy(2*ix+1, n);
  }
}

int main(){
  int n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  
  //rep(i, 0, n) cout << a[i] << " ";
  //cout << endl;

  rep(ix, n/2, 0) maxheapfy(ix, n);
  
  rep(i, 0, n) cout << " " << a[i];
  cout << endl;
  return 0;
}

