#include <bits/stdc++.h>
using namespace std;
int n,q,p;
long long a,b,x,s,wyn;
multiset <long long> szprotki;
vector <long long> uzyte;
multiset <long long>::iterator it;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for(int i=0; i<n; i++){
cin >> x;
szprotki.insert(-x);
s+=x;
}
cin >> q;
for(int i=0; i<q; i++){
cin >> p;
while(!uzyte.empty()){
szprotki.insert(uzyte[uzyte.size()-1]);
uzyte.pop_back();
}
if(p==1){
cin >> a >> b;
wyn=0;
if(b-a>=s){
cout << "-1" << '\n';
continue;
}
while(a<b){
if(szprotki.empty()){
wyn=-1;
break;
}
it=szprotki.upper_bound(-a);
if(it==szprotki.end()){
wyn=-1;
break;
}
a-=*it;
uzyte.push_back(*it);
szprotki.erase(it);
wyn++;
}
cout << wyn << '\n';
}
if(p==2){
cin >> a;
s+=a;
szprotki.insert(-a);
}
if(p==3){
cin >> a;
s-=a;
it=szprotki.find(-a);
if(it!=szprotki.end()) szprotki.erase(it);
}
}
}
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 | #include <bits/stdc++.h> using namespace std; int n,q,p; long long a,b,x,s,wyn; multiset <long long> szprotki; vector <long long> uzyte; multiset <long long>::iterator it; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for(int i=0; i<n; i++){ cin >> x; szprotki.insert(-x); s+=x; } cin >> q; for(int i=0; i<q; i++){ cin >> p; while(!uzyte.empty()){ szprotki.insert(uzyte[uzyte.size()-1]); uzyte.pop_back(); } if(p==1){ cin >> a >> b; wyn=0; if(b-a>=s){ cout << "-1" << '\n'; continue; } while(a<b){ if(szprotki.empty()){ wyn=-1; break; } it=szprotki.upper_bound(-a); if(it==szprotki.end()){ wyn=-1; break; } a-=*it; uzyte.push_back(*it); szprotki.erase(it); wyn++; } cout << wyn << '\n'; } if(p==2){ cin >> a; s+=a; szprotki.insert(-a); } if(p==3){ cin >> a; s-=a; it=szprotki.find(-a); if(it!=szprotki.end()) szprotki.erase(it); } } } |
English