// TEMPLATE START #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<int,int> pii; typedef vector<vi> vvi; typedef vector<pii> vpii; typedef vector<vpii> vvpii; const int MOD = 1000000007; const int INF = 2147483647; const ll LINF = 1e18; const ld PI = 4*atan((ld)1); #define rep(i,a,b) for (int i=(a); i<(b); ++i) #define repd(i,a,b) for (int i=((int)(a)); i>=(b); --i) #define trav(a,x) for (auto& a : x) #define all(u) begin(u), end(u) #define sz(x) (int)x.size() #define xsort(v, m) rep(i, 0, v.size()) m[i] = i; sort(all(m), [=](int i, int j) {return v[i] < v[j];}); sort(v); #define pb push_back #define X first #define Y second #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define PREC(n) cout<<fixed<<setprecision(n); #define db(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl; #define dbv(v) cerr << __LINE__ << ": " << #v << "\n" << v.size() << "\n" << v << "\n"; template <class T, class S> ostream& operator << (ostream& os, const pair<T, S>& p) { os << p.X << " " << p.Y; return os; } template <class T, class S> istream& operator >> (istream& is, pair<T, S>& p) { is >> p.X >> p.Y; return is; } template <class T> ostream& operator << (ostream& os, const vector<T>& v) { rep(i, 0, v.size()) os << v[i] << " \n" [i == v.size() - 1]; return os; } template <class T> istream& operator >> (istream& is, vector<T>& v) { rep(i, 0, v.size()) is >> v[i]; return is; } // TEMPLATE END // SOLUTION START int main() { FAST_IO int n, q, c; ll s, k, w; ll sum = 0; cin >> n; vll v(n); cin >> v; multiset<ll> fish(v.begin(), v.end()); cin >> q; for (int i = 0; i < v.size(); i++) sum += v[i]; while(q--) { cin >> c; if(c == 1) { cin >> s >> k; if (k - s > sum) cout << -1 << "\n"; else { vector<ll> u; bool found = true; int count = 0; while(s < k) { auto r = lower_bound(fish.begin(), fish.end(), s); if (r == fish.begin()) { found = false; break; } else { auto rr = prev(r); s += *rr; u.pb(*rr); fish.erase(rr); count++; } } if(!found) cout << -1 << "\n"; else { cout << count << "\n"; } for (auto& p : u) fish.insert(p); } } else if(c == 2) { cin >> w; fish.insert(w); sum += w; } else if(c == 3) { cin >> w; fish.erase(w); sum -= w; } } return 0; } // SOLUTION END
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // TEMPLATE START #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<int,int> pii; typedef vector<vi> vvi; typedef vector<pii> vpii; typedef vector<vpii> vvpii; const int MOD = 1000000007; const int INF = 2147483647; const ll LINF = 1e18; const ld PI = 4*atan((ld)1); #define rep(i,a,b) for (int i=(a); i<(b); ++i) #define repd(i,a,b) for (int i=((int)(a)); i>=(b); --i) #define trav(a,x) for (auto& a : x) #define all(u) begin(u), end(u) #define sz(x) (int)x.size() #define xsort(v, m) rep(i, 0, v.size()) m[i] = i; sort(all(m), [=](int i, int j) {return v[i] < v[j];}); sort(v); #define pb push_back #define X first #define Y second #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define PREC(n) cout<<fixed<<setprecision(n); #define db(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl; #define dbv(v) cerr << __LINE__ << ": " << #v << "\n" << v.size() << "\n" << v << "\n"; template <class T, class S> ostream& operator << (ostream& os, const pair<T, S>& p) { os << p.X << " " << p.Y; return os; } template <class T, class S> istream& operator >> (istream& is, pair<T, S>& p) { is >> p.X >> p.Y; return is; } template <class T> ostream& operator << (ostream& os, const vector<T>& v) { rep(i, 0, v.size()) os << v[i] << " \n" [i == v.size() - 1]; return os; } template <class T> istream& operator >> (istream& is, vector<T>& v) { rep(i, 0, v.size()) is >> v[i]; return is; } // TEMPLATE END // SOLUTION START int main() { FAST_IO int n, q, c; ll s, k, w; ll sum = 0; cin >> n; vll v(n); cin >> v; multiset<ll> fish(v.begin(), v.end()); cin >> q; for (int i = 0; i < v.size(); i++) sum += v[i]; while(q--) { cin >> c; if(c == 1) { cin >> s >> k; if (k - s > sum) cout << -1 << "\n"; else { vector<ll> u; bool found = true; int count = 0; while(s < k) { auto r = lower_bound(fish.begin(), fish.end(), s); if (r == fish.begin()) { found = false; break; } else { auto rr = prev(r); s += *rr; u.pb(*rr); fish.erase(rr); count++; } } if(!found) cout << -1 << "\n"; else { cout << count << "\n"; } for (auto& p : u) fish.insert(p); } } else if(c == 2) { cin >> w; fish.insert(w); sum += w; } else if(c == 3) { cin >> w; fish.erase(w); sum -= w; } } return 0; } // SOLUTION END |