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

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, q, count;
    unsigned long long a, s, k, w;
    cin >> n;

    multiset<unsigned long long> fish;
    vector<unsigned long long> stack;

    for (int i = 0; i < n; ++i) {
        cin >> a;
        fish.insert(a);
    }

    cin >> q;

    stack.reserve(n + q);

    for (int i = 0; i < q; ++i) {
        cin >> a;
        if (a == 1) {
            //pike attack!
            cin >> s >> k;

            count = 0;
            auto lower = lower_bound(fish.begin(), fish.end(), s);

            while (!fish.empty() && lower != fish.begin() && s < k) {
                lower--;
                while (lower != fish.begin() && (*lower) >= s) {
                    lower--;
                }

                unsigned long long weight = (*lower);
                if (weight >= s) {
                    break;
                }

                s += weight;
                count++;
                stack.push_back(weight);
                fish.erase(lower);
                lower = lower_bound(fish.begin(), fish.end(), s);
            }

            if (s < k) {
                cout << -1 << "\n";
            } else {
                cout << count << "\n";
            }

            while (!stack.empty()) {
                unsigned long long weight = stack[stack.size() - 1];
                fish.insert(weight);
                stack.pop_back();
            }

        } else if (a == 2) {
            //add
            cin >> w;
            fish.insert(w);
        } else if (a == 3) {
            //remove
            cin >> w;
            auto lower = lower_bound(fish.begin(), fish.end(), w);
            fish.erase(lower);
        }
    }

    return 0;
}