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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <map>
#include <set>
#include <vector>

constexpr int64_t C=1ull<<20;
const int MAXQ=100005;
const int MAXN=300005;

class RT {
public:
    void Add(int64_t pos, int64_t v) {
        int64_t x = pos+C;
        while (x > 0) {
            M[x] += v;
            x/=2;
        }
    }
    int64_t Point(int64_t pos) {
        return M[pos+C];
    }
    int64_t Sum(int64_t l, int64_t r) {
        l += C;
        r += C;
        int64_t sum = M[l];
        if (l!=r) sum += M[r];
        while (l/2 < r/2) {
            if (!(l&1)) sum += M[l+1];
            if (r&1) sum += M[r-1];
            l /= 2;
            r /= 2;
        }
        return sum;
    }
    int64_t FindRange(int64_t r, int64_t k)
    {
        r+=C;
        int64_t sum = M[r];
        if (sum >= k) return r-C;
        while (r/2 > 1 && sum < k) {
            if (r&1) sum += M[r-1];
            r /= 2;
        }
        if (sum < k) return 0;

        int64_t l = r*2;
        sum -= M[l];
        while (l < C) {
            if (sum + M[2*l+1] >= k) {
                l = 2*l+1;
            } else {
                l *= 2;
                sum += M[l+1];
            }
        }
        sum += M[l];
        return l-C;
    }
    int64_t M[2*C];
} rt, rc;

std::multiset<int64_t> S;
std::map<int64_t, int64_t> SI, SD;
std::map<int64_t, int64_t> SS, Index;

struct QS {
    int type;
    int64_t a,b;
} Q[MAXQ];

int64_t F[MAXQ+MAXN];
int fn = 0;

struct Range {
    int64_t l, r;
    int64_t sum, count;
};

int64_t Solve(int64_t s, int64_t k) {
    std::vector<Range> used {{-1, -1, 0, 0}};
    int64_t count = 0;

    // std::clog << "SZCZUPAK! " << s << " " << k << std::endl;
    // for (int i=0;i<fn;++i) std::clog << " " << rt.Point(i);
    // std::clog << std::endl;
    while (s < k) {
        auto nextSardynkaIt = S.upper_bound(s);
        int64_t nextSardynka = k;
        if (nextSardynkaIt != S.end()) nextSardynka = *nextSardynkaIt;
        int64_t r = 0;
        if (nextSardynkaIt != S.end()) r = Index[*--nextSardynkaIt];
        else r = Index[F[fn-1]];
        auto l = std::max(
            rt.FindRange(r, k-s),
            rt.FindRange(r, nextSardynka-s+1));
        // std::clog << "# : " << r << " " << rt.FindRange(r, k-s) << " vs " << rt.FindRange(r, nextSardynka-s+1) << " / " << r << std::endl;
        while (l <= used.back().r) {
            s -= used.back().sum;
            count -= used.back().count;
            used.pop_back();
            l = std::max(
                rt.FindRange(r, k-s),
                rt.FindRange(r, nextSardynka-s));
        }
        s += rt.Sum(l, r);
        count += rc.Sum(l, r);
        if (s < k && s <= nextSardynka) return -1;
        // std::clog << count << " EAT";
        // for (int i=l;i<=r;++i) std::clog << " " << F[i];
        // std::clog << std::endl;
        used.push_back({l, r, rt.Sum(l, r), rc.Sum(l, r)});
    }
    return count;
}

int main() {
    std::ios_base::sync_with_stdio(0);
    int n;
    std::cin >> n;
    for (int i=0;i<n;++i) {
        int64_t v;
        std::cin >> v;
        SS[v]++;
        S.insert(v);
    }
    S.insert(0);
    SS[0]++; // guard
    int q;
    std::cin >> q;
    for (int i=0;i<q;++i) {
        std::cin >> Q[i].type;
        if (Q[i].type == 1) {
            std::cin >> Q[i].a >> Q[i].b;
        } else if (Q[i].type == 2) {
            std::cin >> Q[i].a;
            SS[Q[i].a]++;
        } else if (Q[i].type == 3) {
            std::cin >> Q[i].a;
        }
    }
    for (const auto& [w, c] : SS) {
        SI[w] = SD[w] = fn;
        Index[w] = fn+c-1;
        for (int i=0;i<c;++i) {
            F[fn] = w;
            ++fn;
        }
    }
    for (auto w : S) {
        auto& pos = SI[w];
        rt.Add(pos, w);
        rc.Add(pos, 1);
        ++pos;
    }

    for (int i=0;i<q;++i) {
        // std::clog << " :: " << i << std::endl;
        if (Q[i].type == 1) {
            std::cout << Solve(Q[i].a, Q[i].b) << std::endl;
        } else if (Q[i].type == 2) {
            auto& pos = SI[Q[i].a];
            rt.Add(pos, Q[i].a);
            rc.Add(pos, 1);
            ++pos;
            S.insert(Q[i].a);
        } else if (Q[i].type == 3) {
            auto& pos = SD[Q[i].a];
            rt.Add(pos, -Q[i].a);
            rc.Add(pos, -1);
            ++pos;
            S.erase(Q[i].a);
        }
    }
    return 0;
}