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
#include <bits/stdc++.h>
using namespace std;
#define LL long long
/*musi sie udac*/
int n; vector <LL> v;

LL fun(vector <LL> tmp, LL s) {
    LL tmp1 = 0, wynik = tmp.size() - 1;
    while (tmp1 < wynik) {
        LL tmp2 = (tmp1 + wynik) / 2;
        if (s > tmp[tmp2]) tmp1 = tmp2 + 1;
        else wynik = tmp2;
    }
    if (s < tmp[wynik]) return wynik - 1;
    return wynik;
}

LL first(int s, int k) {
    LL wynik = 0; vector <LL> tmp = v;
    if (s <= 1) return -1;
    while (tmp.size() != 0 && s < k) {
        if (s <= tmp[0]) return -1;
        int tmp2 = tmp[fun(tmp, s - 1)]; tmp.erase(tmp.begin() + fun(tmp, s - 1));
        s += tmp2;
        wynik++;
    }
    if (tmp.size() == 0 && s < k) return -1;
    return wynik;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n; v.resize(n);
    for (int i = 0; i < n; i++) {
        LL w; cin >> w; v.push_back(w);
    }
    sort(v.begin(), v.end());
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        LL w; cin >> w;
        if (w == 1) {
            LL s, k; cin >> s >> k;
            LL wynik = first(s, k);
            cout << wynik << endl;
        }
        else if (w == 2) {
            LL ww; cin >> ww; v.push_back(ww);
            int tmp = v.size(); tmp--;
            while (1 <= tmp && v[tmp - 1] > v[tmp]) {
                swap(v[tmp - 1], v[tmp]); tmp--;
            }
        }
        else if (w == 3) {
            LL ww; cin >> ww; v.push_back(ww);
            v.erase(remove(v.begin(), v.end(), ww), v.end());
        }
    }
    return 0;
}