CSES - Shared codeLink to this code:
https://cses.fi/paste/a1ebb1e7fce5dd5f6e4b3d/
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
// 將 vector 換成 multiset,因為 multiset 插入、搜尋、刪除都是 O(log n)
multiset<int> st;
for (int i = 0; i < n; ++i) {
int h;
cin >> h;
st.insert(h);
}
while (m--) {
int t;
cin >> t;
auto it = st.upper_bound(t);
if (it == st.begin()) {
cout << "-1\n";
} else {
cout << *prev(it) << "\n";
st.erase(prev(it));
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
}