#include <bits/stdc++.h>
using namespace std;

template <class T>
struct PQ {
  vector<T> v;
  function<bool(const T&, const T&)> comp;
  
  PQ() : comp(less<T>()) {}
  
  void push(const T& value) {
    v.push_back(value);
    push_heap(v.begin(), v.end(), comp);
  }
  
  void pop() {
    pop_heap(v.begin(), v.end(), comp);
    v.pop_back();
  }
  
  const T& top() {
    return v.front();
  }
  
  bool empty() {
    return v.empty();
  }
  
  void change_comparator(function<bool(const T&, const T&)> other_comp) {
    comp = other_comp;
    make_heap(v.begin(), v.end(), comp);
  }
};

template <class T>
void print(PQ<T> pq) {
  while (!pq.empty()) {
    cout << pq.top() << " ";
    pq.pop();
  }
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  PQ<int> pq;
  for (int i = 1; i <= 10; ++i) {
    pq.push(i);
  }
  print(pq);
  pq.change_comparator(greater<int>());
  print(pq);
  return 0;
}
