#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; multiset<long long> s; cin >> n; long long x; for(int i = 0; i < n; i++) { cin >> x; s.insert(-x); } cin >> q; int type; long long w, k; for(int i = 0; i < q; i++) { cin >> type; switch(type) { case 1: { cin >> w >> k; multiset<long long> removed; int res = 0; while(w < k) { multiset<long long>::iterator p = s.upper_bound(-w); if(p == s.end()) { res = -1; break; } else { w -= *p; res++; removed.insert(*p); s.erase(p); } } for(auto i = removed.begin(); i != removed.end(); i++) s.insert(*i); cout << res << "\n"; } break; case 2: cin >> w; s.insert(-w); break; case 3: cin >> w; s.erase(s.find(-w)); 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 | #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; multiset<long long> s; cin >> n; long long x; for(int i = 0; i < n; i++) { cin >> x; s.insert(-x); } cin >> q; int type; long long w, k; for(int i = 0; i < q; i++) { cin >> type; switch(type) { case 1: { cin >> w >> k; multiset<long long> removed; int res = 0; while(w < k) { multiset<long long>::iterator p = s.upper_bound(-w); if(p == s.end()) { res = -1; break; } else { w -= *p; res++; removed.insert(*p); s.erase(p); } } for(auto i = removed.begin(); i != removed.end(); i++) s.insert(*i); cout << res << "\n"; } break; case 2: cin >> w; s.insert(-w); break; case 3: cin >> w; s.erase(s.find(-w)); break; } } } |