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
#include<bits/stdc++.h>
using namespace std;
#define show if(0)
long long n,w,q,a,s,k,x,err,pocz,kon,j;
vector<long long> V,V2;
vector<long long>::iterator miejsce;
stack<long long> S;
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>w;
    V.push_back(w);
  }
  sort(V.begin(),V.end());
  show{
    cout<<"tab: ";
    for(int i=0;i<(int)V.size();i++)
       cout<<V[i]<<" ";
    cout<<endl;
  }
  cin>>q;
  for(int i=0;i<q;i++){
    cin>>a;
    if(a==1){
      x=0;
      while(!S.empty())
        S.pop();
      cin>>s>>k;
      j=0;
      while(j<V.size()&&s<k){
        show{
          cout<<"tab: ";
          for(int i=0;i<(int)V.size();i++)
             cout<<V[i]<<" ";
          cout<<endl;
          cout<<"s: "<<s<<" k: "<<k<<" j: "<<j<<endl;
        }
        while(j<V.size()&&V[j]<s){
          S.push(V[j]);
          j++;
        }
        //cout<<"SSS "<<S.size()<<endl;
        while(!S.empty()&&(s<=V[j]||j==V.size())&&s<k){
          s+=S.top();
          S.pop();
          x++;
          //cout<<"xx\n";
        }
        if(S.empty()&&(s<=V[j]||j==V.size())&&s<k){
          x=-1;
          break;
        }
      }
      while(!S.empty()&&s<k){
        s+=S.top();
        S.pop();
        x++;
        //cout<<"xxx\n";
      }
      if(s<k)
        x=-1;
      cout<<x<<" "<<endl;
    }
    if(a==2){
      cin>>w;
      V.push_back(w);
      sort(V.begin(),V.end());
    }
    if(a==3){
      cin>>w;
      V.erase(lower_bound(V.begin(),V.end(),w));
    }
    show{
      if(a!=1){
        cout<<"tab: ";
        for(int j=0;j<(int)V.size();j++)
           cout<<V[j]<<" ";
        cout<<endl;
      }
    }
  }
  return 0;
}