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

using namespace std;

int main() {

    int spratsNum;
    scanf("%d\n", &spratsNum);

    multiset<long long> sprats;
    for (int i = 0; i < spratsNum; i++) {
        long long sprat;
        scanf("%lld", &sprat);
        sprats.insert(sprat);
    }

    int eventsNum;
    scanf("%d\n", &eventsNum);

    for (int i = 0; i < eventsNum; i++) {
        int eventType;
        scanf("%d", &eventType);

        if (eventType == 1) {
            long long startWeight;
            long long targetWeight;
            scanf("%lld %lld\n", &startWeight, &targetWeight);

            long long currWeight = startWeight;
            vector<long long> spratsEaten;
            
            while (currWeight < targetWeight) {
                multiset<long long>::iterator spratsIt = sprats.lower_bound(currWeight);
                if (spratsIt == sprats.begin()) {
                    printf("-1\n");
                    break;
                }

                spratsIt--;
                long long toEat = *spratsIt;
                currWeight += toEat;
                spratsEaten.push_back(toEat);
                sprats.erase(spratsIt);
            }

            if (currWeight >= targetWeight) {
                printf("%lu\n", spratsEaten.size());
            }

            for (int j = 0; j < spratsEaten.size(); j++) {
                sprats.insert(spratsEaten[j]);
            }
        } else if (eventType == 2) {
            long long newSprat;
            scanf("%lld\n", &newSprat);

            sprats.insert(newSprat);
        } else if (eventType == 3) {
            long long spratToRemove;
            scanf("%lld\n", &spratToRemove);

            sprats.erase(sprats.find(spratToRemove));
        }
    }

    return 0;
}