#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long long x;
long long suma = 0;
vector<int> szprotki;
for(int i = 0;i<n;i++){
cin>>x;
szprotki.push_back(x);
suma+=x;
}
sort(szprotki.begin(),szprotki.end(),greater<int>());
int q;
cin>>q;
int a,b;
int z;
int e,ile,k;
vector<int> nowe;
for(int i = 0;i<q;i++){
cin>>x;
if(x == 2){
cin>>e;
suma += e;
z = 0;
for(int j = 0;j<szprotki.size();j++){
if(szprotki[j]<e){
break;
}
z ++;
}
szprotki.insert(szprotki.begin() + z,e);
}
if(x == 3){
cin>>e;
for(int j = 0;j<szprotki.size();j++){
if(szprotki[j] == e){
szprotki.erase(szprotki.begin() + j);
suma -= szprotki[j];
break;
}
}
}
if(x == 1){
cin>>a>>b;
if(b - a >suma){
cout<<-1;
}
else{
ile = 0;
nowe.clear();
for(int j =0;j<szprotki.size();j++){
nowe.push_back(szprotki[j]);
}
while(a<b){
k = 0;
while(k<nowe.size()){
if(nowe[k] < a){
a += nowe[k];
//cout<<nowe[k]<<"\n";
nowe.erase(nowe.begin()+k);
k--;
ile++;
break;
}
k++;
}
if(k == nowe.size()){
ile = -1;
break;
}
}
cout<<ile;
}
cout<<"\n";
}
}
}
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 | #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; long long x; long long suma = 0; vector<int> szprotki; for(int i = 0;i<n;i++){ cin>>x; szprotki.push_back(x); suma+=x; } sort(szprotki.begin(),szprotki.end(),greater<int>()); int q; cin>>q; int a,b; int z; int e,ile,k; vector<int> nowe; for(int i = 0;i<q;i++){ cin>>x; if(x == 2){ cin>>e; suma += e; z = 0; for(int j = 0;j<szprotki.size();j++){ if(szprotki[j]<e){ break; } z ++; } szprotki.insert(szprotki.begin() + z,e); } if(x == 3){ cin>>e; for(int j = 0;j<szprotki.size();j++){ if(szprotki[j] == e){ szprotki.erase(szprotki.begin() + j); suma -= szprotki[j]; break; } } } if(x == 1){ cin>>a>>b; if(b - a >suma){ cout<<-1; } else{ ile = 0; nowe.clear(); for(int j =0;j<szprotki.size();j++){ nowe.push_back(szprotki[j]); } while(a<b){ k = 0; while(k<nowe.size()){ if(nowe[k] < a){ a += nowe[k]; //cout<<nowe[k]<<"\n"; nowe.erase(nowe.begin()+k); k--; ile++; break; } k++; } if(k == nowe.size()){ ile = -1; break; } } cout<<ile; } cout<<"\n"; } } } |
English