#include <iostream> #include <set> using namespace std; multiset<long long> S1; multiset<long long> S2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i=0; i<n; i++) { long long w; cin >> w; S1.insert(w); } int q; cin >> q; for (int i=0; i<q; i++) { int r; cin >> r; if (r == 1) { long long s, k; cin >> s >> k; S2 = S1; multiset<long long>::iterator it1; multiset<long long>::iterator it2; bool con = true; int cnt = 0; while (!S2.empty() && con && s < k) { it1 = S2.lower_bound(s); it2 = it1; if (it2 != S2.begin()) { it2--; } if ((it1 == S2.end() && *it2 >= s) || it1 == S2.begin()) { con = false; } else { s += *it2; S2.erase(it2); cnt++; } } if (!con || s < k) { cout << "-1\n"; } else { cout << cnt << '\n'; } } else { long long w; cin >> w; if (r == 2) { S1.insert(w); } else { multiset<long long>::iterator it; it = S1.find(w); S1.erase(it); } } } return 0; }
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <iostream> #include <set> using namespace std; multiset<long long> S1; multiset<long long> S2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i=0; i<n; i++) { long long w; cin >> w; S1.insert(w); } int q; cin >> q; for (int i=0; i<q; i++) { int r; cin >> r; if (r == 1) { long long s, k; cin >> s >> k; S2 = S1; multiset<long long>::iterator it1; multiset<long long>::iterator it2; bool con = true; int cnt = 0; while (!S2.empty() && con && s < k) { it1 = S2.lower_bound(s); it2 = it1; if (it2 != S2.begin()) { it2--; } if ((it1 == S2.end() && *it2 >= s) || it1 == S2.begin()) { con = false; } else { s += *it2; S2.erase(it2); cnt++; } } if (!con || s < k) { cout << "-1\n"; } else { cout << cnt << '\n'; } } else { long long w; cin >> w; if (r == 2) { S1.insert(w); } else { multiset<long long>::iterator it; it = S1.find(w); S1.erase(it); } } } return 0; } |