#include <bits/stdc++.h> using namespace std; using ll = long long; ll query(multiset<ll> fish, ll s, ll k) { int res = 0; while (s < k && !fish.empty()) { auto it = fish.lower_bound(s); if (it == fish.begin()) { break; } it--; s += *it; fish.erase(it); res++; } if (s < k) { return -1; } else { return res; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; multiset<ll> fish; for (int i = 0; i < n; i++) { ll w; cin >> w; fish.insert(w); } int q; cin >> q; while (q--) { ll t, s, k, w; cin >> t; if (t == 1) { cin >> s >> k; cout << query(fish, s, k) << "\n"; } else if (t == 2) { cin >> w; fish.insert(w); } else { // t == 3 cin >> w; auto it = fish.find(w); fish.erase(it); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <bits/stdc++.h> using namespace std; using ll = long long; ll query(multiset<ll> fish, ll s, ll k) { int res = 0; while (s < k && !fish.empty()) { auto it = fish.lower_bound(s); if (it == fish.begin()) { break; } it--; s += *it; fish.erase(it); res++; } if (s < k) { return -1; } else { return res; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; multiset<ll> fish; for (int i = 0; i < n; i++) { ll w; cin >> w; fish.insert(w); } int q; cin >> q; while (q--) { ll t, s, k, w; cin >> t; if (t == 1) { cin >> s >> k; cout << query(fish, s, k) << "\n"; } else if (t == 2) { cin >> w; fish.insert(w); } else { // t == 3 cin >> w; auto it = fish.find(w); fish.erase(it); } } } |