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
#include <iostream>
#include <deque>
#include <array>
#include <vector>
#include <tuple>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <cassert>
using namespace std;

template<typename A, typename B>
ostream& operator<<(ostream& o, const pair<A,B>& d){
    o << "(" << d.first << ", " << d.second << ")";
    return o;
}

template<typename T>
ostream& operator<<(ostream& o, const vector<T>& d){
    o << "[";
    for (const T&i:d) o << i << ", ";
    o  <<"]";
    return o;
}

using L = long long;

map<int, int> data_;

void add_fish(L a) {
    data_[a] ++;
}

void remove_fish(L a) {
    data_[a] --;
}

L next_beq(L than) {
    auto it = data_.lower_bound(than);
    if (it == data_.end()) return -1;
    return it->first;
}

vector<pair<L, L>> rollback;

pair<L, int> iterate_from_until_sum(L curr, L target_sum) {
    L cum = 0;
    int count = 0;

    auto it = data_.lower_bound(curr);

    while (true) {
        if (target_sum <= 0) break;

        if (it == data_.begin()) break;
        it --;

        int pos = it->first;

        int need_count = ((target_sum + pos - 1) / pos);
        int take = min(need_count, data_[pos]);

        //if (take != 0)
        //    cerr << "  zjadam waga=" << pos << " num=" << take << " zostalo=" << target_sum << endl;

        data_[pos] -= take;
        rollback.emplace_back(pos, take);

        count += take;
        cum += L(take) * pos;
        target_sum -= L(take) * pos;
    }
    return make_pair(cum, count);
}

L solve(L curr, L target) {
    //cerr << "solve " << curr << " " << target << endl;
    L res = 0;
    while (curr < target) {
        //cerr << "  " << curr << endl;
        L bigger = next_beq(curr);
        L curr_target = target;
        if (bigger != -1) curr_target = min(bigger + 1, curr_target);
        //cerr << "  bigger " << bigger << " curr_target " << curr_target << endl;

        L need_to_add = curr_target - curr;
        assert(need_to_add > 0);

        auto rr = iterate_from_until_sum(curr, need_to_add);
        L added = rr.first;
        res += rr.second;

        //cerr << "  #" << rr.first << " " << rr.second << endl;
        if (added == 0) { res = -1; break; }

        curr += added;
        //cerr << "  penalty " <<curr-1 << " added " << added<<endl;
    }

    for (auto p : rollback) data_[p.first] += p.second;
    rollback.clear();

    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    while(n--) { int v; cin >> v; add_fish(v); }
    int q;
    cin >> q;
    while (q--) {
        int c;
        cin >> c;
        if (c == 1) {
            L s, k;
            cin >> s >> k;
            cout << solve(s, k) << endl;
        } else if (c == 2) {
            L i;
            cin >> i;
            add_fish(i);
        } else if (c == 3) {
            L i;
            cin >> i;
            remove_fish(i);
        }
    }
}