#include <bits/stdc++.h>
using namespace std;
int solution(const vector<int> &a, const vector<int> &b, int m) {
const int n = a.size();
set<int> s, fixed;
set<pair<int, int>> smaller, largea, largeb;
for (int i = 0; i < n; ++i) {
if (a[i] < b[i]) {
smaller.insert({a[i], i});
} else if (a[i] == b[i]) {
fixed.insert(a[i]);
} else {
largea.insert({a[i], i});
largeb.insert({b[i], i});
}
s.insert(a[i]);
s.insert(b[i]);
}
int r = INT_MAX;
set<pair<int, int>> topa, topb;
for (int x : s) {
// we want all values >= x
while (!smaller.empty() && smaller.begin()->first < x) {
if (m-- == 0) return r;
fixed.insert(b[smaller.begin()->second]);
smaller.erase(smaller.begin());
}
if (!fixed.empty() && *fixed.begin() < x) return r;
if (!largea.empty() && largea.begin()->first < x) return r;
if (!topa.empty() && topa.begin()->first < x) return r;
while (!largeb.empty() && largeb.begin()->first < x) {
const int ind = largeb.begin()->second;
fixed.insert(a[ind]);
largea.erase({a[ind], ind});
largeb.erase(largeb.begin());
}
while (!topb.empty() && topb.begin()->first < x) {
const int ind = topb.begin()->second;
fixed.insert(a[ind]);
topa.erase({a[ind], ind});
topb.erase(topb.begin());
}
while (topa.size() > m) {
largea.insert(*topa.begin());
const int ind = topa.begin()->second;
largeb.insert({b[ind], ind});
topa.erase(topa.begin());
topb.erase({b[ind], ind});
}
while (topa.size() < m && !largea.empty()) {
topa.insert(*largea.rbegin());
const int ind = largea.rbegin()->second;
topb.insert({b[ind], ind});
largea.erase({a[ind], ind});
largeb.erase({b[ind], ind});
}
int m = INT_MIN;
if (!smaller.empty()) {
m = max(m, smaller.rbegin()->first);
}
if (!fixed.empty()) {
m = max(m, *fixed.rbegin());
}
if (!largea.empty()) {
m = max(m, largea.rbegin()->first);
}
if (!topb.empty()) {
m = max(m, topb.rbegin()->first);
}
r = min(r, m - x);
}
return r;
}
int main() {
cout << solution({1, 2, 17, 16, 9}, {3, 4, 5, 6, 11}, 2) << endl;
cout << solution({1, 100}, {99999, 100000}, 2) << endl;
cout << solution({1, 100}, {99999, 100000}, 1) << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgc29sdXRpb24oY29uc3QgdmVjdG9yPGludD4gJmEsIGNvbnN0IHZlY3RvcjxpbnQ+ICZiLCBpbnQgbSkgewogICAgY29uc3QgaW50IG4gPSBhLnNpemUoKTsKICAgIHNldDxpbnQ+IHMsIGZpeGVkOwogICAgc2V0PHBhaXI8aW50LCBpbnQ+PiBzbWFsbGVyLCBsYXJnZWEsIGxhcmdlYjsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgaWYgKGFbaV0gPCBiW2ldKSB7CiAgICAgICAgICAgIHNtYWxsZXIuaW5zZXJ0KHthW2ldLCBpfSk7CiAgICAgICAgfSBlbHNlIGlmIChhW2ldID09IGJbaV0pIHsKICAgICAgICAgICAgZml4ZWQuaW5zZXJ0KGFbaV0pOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGxhcmdlYS5pbnNlcnQoe2FbaV0sIGl9KTsKICAgICAgICAgICAgbGFyZ2ViLmluc2VydCh7YltpXSwgaX0pOwogICAgICAgIH0KICAgICAgICBzLmluc2VydChhW2ldKTsKICAgICAgICBzLmluc2VydChiW2ldKTsKICAgIH0KICAgIGludCByID0gSU5UX01BWDsKICAgIHNldDxwYWlyPGludCwgaW50Pj4gdG9wYSwgdG9wYjsKICAgIGZvciAoaW50IHggOiBzKSB7CiAgICAgICAgLy8gd2Ugd2FudCBhbGwgdmFsdWVzID49IHgKICAgICAgICB3aGlsZSAoIXNtYWxsZXIuZW1wdHkoKSAmJiBzbWFsbGVyLmJlZ2luKCktPmZpcnN0IDwgeCkgewogICAgICAgICAgICBpZiAobS0tID09IDApIHJldHVybiByOwogICAgICAgICAgICBmaXhlZC5pbnNlcnQoYltzbWFsbGVyLmJlZ2luKCktPnNlY29uZF0pOwogICAgICAgICAgICBzbWFsbGVyLmV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hcmdlYi5pbnNlcnQoe2JbaW5kXSwgaW5kfSk7CiAgICAgICAgICAgIHRvcGEuZXJhc2UodG9wYS5iZWdpbigpKTsKICAgICAgICAgICAgdG9wYi5lcmFzZSh7YltpbmRdLCBpbmR9KTsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKHRvcGEuc2l6ZSgpIDwgbSAmJiAhbGFyZ2VhLmVtcHR5KCkpIHsKICAgICAgICAgICAgdG9wYS5pbnNlcnQoKmxhcmdlYS5yYmVnaW4oKSk7CiAgICAgICAgICAgIGNvbnN0IGludCBpbmQgPSBsYXJnZWEucmJlZ2luKCktPnNlY29uZDsKICAgICAgICAgICAgdG9wYi5pbnNlcnQoe2JbaW5kXSwgaW5kfSk7CiAgICAgICAgICAgIGxhcmdlYS5lcmFzZSh7YVtpbmRdLCBpbmR9KTsKICAgICAgICAgICAgbGFyZ2ViLmVyYXNlKHtiW2luZF0sIGluZH0pOwogICAgICAgIH0KICAgICAgICBpbnQgbSA9IElOVF9NSU47CiAgICAgICAgaWYgKCFzbWFsbGVyLmVtcHR5KCkpIHsKICAgICAgICAgICAgbSA9IG1heChtLCBzbWFsbGVyLnJiZWdpbigpLT5maXJzdCk7CiAgICAgICAgfQogICAgICAgIGlmICghZml4ZWQuZW1wdHkoKSkgewogICAgICAgICAgICBtID0gbWF4KG0sICpmaXhlZC5yYmVnaW4oKSk7CiAgICAgICAgfQogICAgICAgIGlmICghbGFyZ2VhLmVtcHR5KCkpIHsKICAgICAgICAgICAgbSA9IG1heChtLCBsYXJnZWEucmJlZ2luKCktPmZpcnN0KTsKICAgICAgICB9CiAgICAgICAgaWYgKCF0b3BiLmVtcHR5KCkpIHsKICAgICAgICAgICAgbSA9IG1heChtLCB0b3BiLnJiZWdpbigpLT5maXJzdCk7CiAgICAgICAgfQogICAgICAgIHIgPSBtaW4ociwgbSAtIHgpOwogICAgfQogICAgCiAgICByZXR1cm4gcjsKfQoKaW50IG1haW4oKSB7CiAgICBjb3V0IDw8IHNvbHV0aW9uKHsxLCAyLCAxNywgMTYsIDl9LCB7MywgNCwgNSwgNiwgMTF9LCAyKSA8PCBlbmRsOwogICAgY291dCA8PCBzb2x1dGlvbih7MSwgMTAwfSwgezk5OTk5LCAxMDAwMDB9LCAyKSA8PCBlbmRsOwogICAgY291dCA8PCBzb2x1dGlvbih7MSwgMTAwfSwgezk5OTk5LCAxMDAwMDB9LCAxKSA8PCBlbmRsOwogICAgcmV0dXJuIDA7Cn0=