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
#include<bits/stdc++.h>
using namespace std;

const int N = 300 * 1000 + 7;
int n, q, b, cnt;
long long a, masa, k, suma;
vector<long long>ryby;
priority_queue<int>Q;

int bs(long long x) {
    int L = 0, P = 2*(ryby.size()-1), S = 0;
    while(L < P) {
        S = (L + P + 1) / 2;
        if(ryby[S] >= x) P = S - 1;
        else L = S;
    }
    return L;
}


bool ok() {
    cnt = 0;
    if(masa >= k) return true;
    if(k - masa > suma || masa == 1) return false;
    while(!Q.empty()) Q.pop();
    for(int i = 0; i < ryby.size() && masa < k; i++) {
        while(i < ryby.size()-1 && masa > ryby[i+1]) {
            Q.push(i);
            i++;
        }
        if(i == 0 && masa <= ryby[0]) return false;
        masa += ryby[i], cnt++;
        while(!Q.empty() && masa < k && (masa <= ryby[i+1] || i == ryby.size()-1)) {
            masa += ryby[Q.top()], cnt++;
            Q.pop();
        }
        if(masa <= ryby[i+1] && masa < k) return false;
    }
    return true;
}

int main() {

    ios_base::sync_with_stdio(0);
    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> a;
        suma += a;
        ryby.push_back(a);
    }
    sort(ryby.begin(), ryby.end());

    cin >> q;
    for(int z = 0; z < q; z++) {
        cin >> b;
        if(b == 1) {
            cin >> masa >> k;
            if(ok()) cout << cnt << "\n";
            else cout << "-1\n";
        }
        if(b == 2) {
            cin >> a;
            suma += a;
            if(ryby.empty() ||  a >= ryby[ryby.size()-1]) ryby.push_back(a);
            else if(ryby[0] >= a) ryby.insert(ryby.begin(), a);
            else ryby.insert(ryby.begin() + bs(a)+1, a);
        }
        if(b == 3) {
            cin >> a;
            suma -= a;
            if(a >= ryby[ryby.size()-1]) ryby.pop_back();
            else if(ryby[0] != a) ryby.erase(ryby.begin() + bs(a) + 1);
            else ryby.erase(ryby.begin() + bs(a));
        }
    }
}