#include <iostream> #include <set> #include <vector> using namespace std; multiset<long long, greater<long long> > szprotki; multiset<long long, greater<long long> >::iterator sit; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i=0;i<n;i++) { long long s; cin >> s; szprotki.insert(s); } int q; cin >> q; while (q--) { int t; long long s,k,w; cin >> t; if (t==2) { cin >> w; szprotki.insert(w); } else if (t==3) { cin >> w; szprotki.erase(w); } else { cin >> s >> k; vector<long long> temp; sit = szprotki.begin(); long long ans=0; while (s<k) { sit = szprotki.upper_bound(s); if (sit!=szprotki.end()) { s += *sit; temp.push_back(*sit); szprotki.erase(sit); ans++; } else { ans=-1; break; } } for (auto &it:temp) szprotki.insert(it); cout << ans << endl; } } 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 | #include <iostream> #include <set> #include <vector> using namespace std; multiset<long long, greater<long long> > szprotki; multiset<long long, greater<long long> >::iterator sit; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i=0;i<n;i++) { long long s; cin >> s; szprotki.insert(s); } int q; cin >> q; while (q--) { int t; long long s,k,w; cin >> t; if (t==2) { cin >> w; szprotki.insert(w); } else if (t==3) { cin >> w; szprotki.erase(w); } else { cin >> s >> k; vector<long long> temp; sit = szprotki.begin(); long long ans=0; while (s<k) { sit = szprotki.upper_bound(s); if (sit!=szprotki.end()) { s += *sit; temp.push_back(*sit); szprotki.erase(sit); ans++; } else { ans=-1; break; } } for (auto &it:temp) szprotki.insert(it); cout << ans << endl; } } return 0; } |