https://cses.fi/paste/aa9e4b0b9fd315556e287d/
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; vector<int> vec(n); for (int &i : vec) cin >> i; sort(vec.begin(), vec.end()); while (m--) { int t; cin >> t; auto it = upper_bound(vec.begin(), vec.end(), t); if (it == vec.begin()) { cout << "-1\n"; } else { cout << *prev(it) << "\n"; vec.erase(prev(it)); } } } int main() { ios::sync_with_stdio(0), cin.tie(0); solve(); }