#include <iostream> #include <vector> #include <set> using namespace std; int main() { ios_base::sync_with_stdio(false); unsigned long n; int q; cin >> n; multiset<long long int> s; multiset<long long int> s2; long long int w; for (int i = 0; i < n; i++) { cin >> w; s.insert(w); } cin >> q; int a; long long int b, c; int counter; for (int i = 0; i < q; i++) { cin >> a; if (a == 1) { s2.clear(); counter = 0; cin >> b >> c; auto it = s.begin(); while (true) { if (b >= c) { cout << counter << "\n"; break; } while (it != s.end()) { if (*it >= b) { break; } s2.insert(*it); it++; } if (!s2.empty()) { b += *(s2.rbegin()); counter++; auto it2 = s2.end(); --it2; s2.erase(it2); } else { cout << "-1\n"; break; } } } if (a == 2) { cin >> b; s.insert(b); } if (a == 3) { cin >> b; s.erase(s.find(b)); } } 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 | #include <iostream> #include <vector> #include <set> using namespace std; int main() { ios_base::sync_with_stdio(false); unsigned long n; int q; cin >> n; multiset<long long int> s; multiset<long long int> s2; long long int w; for (int i = 0; i < n; i++) { cin >> w; s.insert(w); } cin >> q; int a; long long int b, c; int counter; for (int i = 0; i < q; i++) { cin >> a; if (a == 1) { s2.clear(); counter = 0; cin >> b >> c; auto it = s.begin(); while (true) { if (b >= c) { cout << counter << "\n"; break; } while (it != s.end()) { if (*it >= b) { break; } s2.insert(*it); it++; } if (!s2.empty()) { b += *(s2.rbegin()); counter++; auto it2 = s2.end(); --it2; s2.erase(it2); } else { cout << "-1\n"; break; } } } if (a == 2) { cin >> b; s.insert(b); } if (a == 3) { cin >> b; s.erase(s.find(b)); } } return 0; } |