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
#include<iostream>
#include <algorithm>
#include<map>

#define T long long int

int solve(std::map<T, int> sprats, T sum, T s, T k) {
    std::map<T, int>::iterator ss;
    int ret = 0;
//    std::cout << "---" << std::endl;
//    for(auto s = sprats.begin(); s != sprats.end(); s++) {
//        std::cout << s->first << " " << s->second << std::endl;
//    }
    if(s+sum < k) {
        ret = -1;
        return ret;
    }
    while(s < k) {
        if(!sprats.size()) {
            ret = -1;
            break;
        }
        ss = sprats.lower_bound(s-1);
        if(ss == sprats.end()) {
            ss--;
        } else {
            if(ss->first > s-1) {
                if(ss == sprats.begin()) {
                    ret = -1;
                    break;
                } else {
                    ss--;
                }
            }
        }
        s += ss->first;
//        std::cout << "Eaten: " << ss->first << " s: " << s << std::endl;
        ss->second--;
        if(!ss->second) {
            sprats.erase(ss);
        }
        ret++;
    }
    return ret;
}

int main() {
    std::map<T, int> sprats;
    int n, q, op;
    T w, s, k, sum=0;
    std::cin >> n;
    while(n--) {
        std::cin >> w;
        sprats[w]++;
        sum += w;
    }
    std::cin >> q;
    while(q--) {
        std::cin >> op >> s;
        if(op == 1) {
            std::cin >> k;
            std::cout << solve(sprats, sum, s, k) << std::endl;
        }
        if(op == 2) {
            sprats[s]++;
            sum += s;
        }
        if(op == 3) {
            sprats[s]--;
            if(!sprats[s]) {
                sprats.erase(s);
            }
            sum -= s;
        }
        
    }
    return 0;
}