#include <bits/stdc++.h>
using namespace std;
multiset <long long> tab;
vector <long long> urz;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, x, t, typ, w, s, suma;
int ile;
cin >> n;
tab.insert(0);
for(int i=0;i<n;i++)
{
cin >> x;
suma+=x;
tab.insert(-x);
}
cin >> t;
auto b = tab.upper_bound(0);
long long waga, temp;
for(int i=0;i<t;i++)
{
cin >> typ;
if(typ==1)
{
cin >> waga >> s;
if(s>suma)
{
cout << -1 << endl;
}
else
{
ile=0;
while(waga<s)
{
//cout <<"waga " << waga << endl;
b=tab.upper_bound(-waga);
temp=-(*b);
//cout << "TEMP " << temp << endl;
if(temp==0)
{
cout << -1 << endl;
ile=-1;
break;
}
urz.push_back(*b);
tab.erase(b);
waga+=temp;
ile++;
}
if(ile!=-1)
cout << ile << endl;
for(int i=0;i<urz.size(); i++)
{
tab.insert(urz[i]);
}
urz.clear();
}
}
else if(typ==2)
{
cin >> w;
suma+=w;
tab.insert(-w);
}
else
{
cin >> w;
b=tab.lower_bound(-w);
suma-=w;
tab.erase(b);
}
}
}
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 | #include <bits/stdc++.h> using namespace std; multiset <long long> tab; vector <long long> urz; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, x, t, typ, w, s, suma; int ile; cin >> n; tab.insert(0); for(int i=0;i<n;i++) { cin >> x; suma+=x; tab.insert(-x); } cin >> t; auto b = tab.upper_bound(0); long long waga, temp; for(int i=0;i<t;i++) { cin >> typ; if(typ==1) { cin >> waga >> s; if(s>suma) { cout << -1 << endl; } else { ile=0; while(waga<s) { //cout <<"waga " << waga << endl; b=tab.upper_bound(-waga); temp=-(*b); //cout << "TEMP " << temp << endl; if(temp==0) { cout << -1 << endl; ile=-1; break; } urz.push_back(*b); tab.erase(b); waga+=temp; ile++; } if(ile!=-1) cout << ile << endl; for(int i=0;i<urz.size(); i++) { tab.insert(urz[i]); } urz.clear(); } } else if(typ==2) { cin >> w; suma+=w; tab.insert(-w); } else { cin >> w; b=tab.lower_bound(-w); suma-=w; tab.erase(b); } } } |
English