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

struct Pike {
    long long currentWeight;
    long long expectedWeight;
};

using Pond = std::multiset<long long int, std::greater<>>;

void handlePike(Pond &);

int main() {
    Pond pond;
    int n, q;
    long long sprat;
    std::cin >> n;
    for (int i = 0; i < n; ++i) {
        std::cin >> sprat;
        pond.insert(sprat);
    }

    std::cin >> q;
    for (int i = 0; i < q; ++i) {
        int operation;
        std::cin >> operation;
        switch (operation) {
            case 1:
                handlePike(pond);
                break;
            case 2:
                std::cin >> sprat;
                pond.insert(sprat);
                break;
            case 3:
                std::cin >> sprat;
                pond.erase(pond.find(sprat));
        }
    }

    return 0;
}

void handlePike(Pond &pond) {
    Pike pike;
    std::cin >> pike.currentWeight >> pike.expectedWeight;
    Pond tempPond;
    while (pike.currentWeight < pike.expectedWeight and not pond.empty()) {
        auto heaviestSprat = pond.upper_bound(pike.currentWeight);
        if (heaviestSprat == pond.end()) {
            break;
        } else {
            pike.currentWeight += *heaviestSprat;
            tempPond.insert(pond.extract(heaviestSprat));
        }
    }
    if (pike.currentWeight < pike.expectedWeight) {
        std::cout << -1 << '\n';
    } else {
        std::cout << tempPond.size() << '\n';
    }
    pond.merge(tempPond);
}