#include <bits/stdc++.h> using namespace std; int n, q; vector <long long> szp; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { long long a; cin >> a; szp.push_back(a); } sort(szp.begin(), szp.end()); cin >> q; for (int f = 0, t; f < q; f++) { long long s, k, w; cin >> t; if (t == 1) { bitset <300005> B = 0; cin >> s >> k; bool sad = false, happy = false; for (int i = 0; i < szp.size(); i++) { if (s >= k) break; if (s <= szp[i]) { for (int j = i-1; j >= 0 && s <= szp[i]; j--) { if (!B[j]) { s += szp[j]; B[j] = true; if (s >= k) { happy = true; break; } } } if (happy) break; if (s <= szp[i]) { sad = true; break; } } } for (int i = szp.size()-1; s < k && i >= 0; i--) if (!B[i]) { s += szp[i]; B[i] = true; } if (sad || s < k) cout << "-1\n"; else cout << B.count() << '\n'; } if (t == 2) { cin >> w; szp.push_back(w); sort(szp.begin(), szp.end()); } if (t == 3) { cin >> w; int x = 0; while (szp[x] != w) x++; szp.erase(szp.begin()+x); } } }
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <bits/stdc++.h> using namespace std; int n, q; vector <long long> szp; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { long long a; cin >> a; szp.push_back(a); } sort(szp.begin(), szp.end()); cin >> q; for (int f = 0, t; f < q; f++) { long long s, k, w; cin >> t; if (t == 1) { bitset <300005> B = 0; cin >> s >> k; bool sad = false, happy = false; for (int i = 0; i < szp.size(); i++) { if (s >= k) break; if (s <= szp[i]) { for (int j = i-1; j >= 0 && s <= szp[i]; j--) { if (!B[j]) { s += szp[j]; B[j] = true; if (s >= k) { happy = true; break; } } } if (happy) break; if (s <= szp[i]) { sad = true; break; } } } for (int i = szp.size()-1; s < k && i >= 0; i--) if (!B[i]) { s += szp[i]; B[i] = true; } if (sad || s < k) cout << "-1\n"; else cout << B.count() << '\n'; } if (t == 2) { cin >> w; szp.push_back(w); sort(szp.begin(), szp.end()); } if (t == 3) { cin >> w; int x = 0; while (szp[x] != w) x++; szp.erase(szp.begin()+x); } } } |