#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0ZW1wbGF0ZSA8Y2xhc3MgVD4Kc3RydWN0IFBRIHsKICB2ZWN0b3I8VD4gdjsKICBmdW5jdGlvbjxib29sKGNvbnN0IFQmLCBjb25zdCBUJik+IGNvbXA7CiAgCiAgUFEoKSA6IGNvbXAobGVzczxUPigpKSB7fQogIAogIHZvaWQgcHVzaChjb25zdCBUJiB2YWx1ZSkgewogICAgdi5wdXNoX2JhY2sodmFsdWUpOwogICAgcHVzaF9oZWFwKHYuYmVnaW4oKSwgdi5lbmQoKSwgY29tcCk7CiAgfQogIAogIHZvaWQgcG9wKCkgewogICAgcG9wX2hlYXAodi5iZWdpbigpLCB2LmVuZCgpLCBjb21wKTsKICAgIHYucG9wX2JhY2soKTsKICB9CiAgCiAgY29uc3QgVCYgdG9wKCkgewogICAgcmV0dXJuIHYuZnJvbnQoKTsKICB9CiAgCiAgYm9vbCBlbXB0eSgpIHsKICAgIHJldHVybiB2LmVtcHR5KCk7CiAgfQogIAogIHZvaWQgY2hhbmdlX2NvbXBhcmF0b3IoZnVuY3Rpb248Ym9vb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