#include <algorithm> #include <iostream> #include <set> #include <vector> using namespace std; typedef long long LL; int main() { ios_base::sync_with_stdio(false); LL n, x, q, s, k, ct; cin >> n; vector<LL> szprotki; auto it = szprotki.begin(); vector<LL> v; for (int i = 0; i < n; ++i) { cin >> x; szprotki.push_back(x); } sort(szprotki.begin(), szprotki.end()); cin >> q; while (q--) { cin >> x; switch (x) { case 1: cin >> s >> k; ct = 0; it = szprotki.begin(); while (s < k) { for (; it != szprotki.end() && *it < s; ++it) { v.push_back(*it); push_heap(v.begin(), v.end()); } if (v.empty()) { break; } pop_heap(v.begin(), v.end()); LL r = v.back(); v.pop_back(); s += r; ct++; } v.clear(); if (s < k) { cout << -1 << '\n'; } else { cout << ct << '\n'; } break; case 2: cin >> k; szprotki.insert( lower_bound(szprotki.begin(), szprotki.end(), k), k); break; case 3: cin >> k; szprotki.erase( lower_bound(szprotki.begin(), szprotki.end(), k)); break; } } }
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 | #include <algorithm> #include <iostream> #include <set> #include <vector> using namespace std; typedef long long LL; int main() { ios_base::sync_with_stdio(false); LL n, x, q, s, k, ct; cin >> n; vector<LL> szprotki; auto it = szprotki.begin(); vector<LL> v; for (int i = 0; i < n; ++i) { cin >> x; szprotki.push_back(x); } sort(szprotki.begin(), szprotki.end()); cin >> q; while (q--) { cin >> x; switch (x) { case 1: cin >> s >> k; ct = 0; it = szprotki.begin(); while (s < k) { for (; it != szprotki.end() && *it < s; ++it) { v.push_back(*it); push_heap(v.begin(), v.end()); } if (v.empty()) { break; } pop_heap(v.begin(), v.end()); LL r = v.back(); v.pop_back(); s += r; ct++; } v.clear(); if (s < k) { cout << -1 << '\n'; } else { cout << ct << '\n'; } break; case 2: cin >> k; szprotki.insert( lower_bound(szprotki.begin(), szprotki.end(), k), k); break; case 3: cin >> k; szprotki.erase( lower_bound(szprotki.begin(), szprotki.end(), k)); break; } } } |