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
#include <iostream>
#include <set>

using namespace std;

main() {
    int n, q, t;
    long long w, k;
    multiset<long long> s;
    multiset<long long>::iterator it;
    cin >> n;
    for (int i=0; i<n; i++) {
        cin >> w;
        s.insert(w);
    }
    cin >> q;
    for (int i=0; i<q; i++) {
        cin >> t >> w;
        if (t == 1) {
            cin >> k;
            int count = 0;
            multiset<long long> s1;
            while (w < k) {
                if (s.empty())
                    break;
                it = s.lower_bound(w);
                if (it == s.begin())
                    break;
                it--;
                w += *it;
                s1.insert(*it);
                s.erase(it);
                count++;
            }   
            if (w < k) 
                cout << -1 << endl;
            else
                cout << count << endl;
            s.insert(s1.begin(), s1.end());
        }
        if (t == 2)
            s.insert(w);
        if (t == 3)
            s.erase(s.find(w));
    }

    return 0;
}