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

using namespace std;

typedef long long int lint;

struct Event {
    int type;
    lint w;
    lint s;
    lint k;
};

void read_data(vector<lint>& w, vector<Event>& events) {
    int n; cin >> n;
    for (int i = 0; i < n; ++i) {
        int wi; cin >> wi;
        w.push_back(wi);
    }

    int q; cin >> q;
    for (int i = 0; i < q; ++i) {
        Event ev;
        cin >> ev.type;
        if (ev.type == 1) {
            cin >> ev.s >> ev.k;
        } else {
            cin >> ev.w;
        }
        events.push_back(ev);
    }
}

class Mlotek {
public:
    typedef void (Mlotek::*eventHandler)(Event);

    vector<lint>& w;
    vector<Event>& events;
    multiset<lint> ws;

    map<int, eventHandler> eventHandlers = {
        {1, &Mlotek::handleSimulation},
        {2, &Mlotek::handleAdd},
        {3, &Mlotek::handleRemove},
    };

    Mlotek(vector<lint>& _w, vector<Event>& _events)
        : w(_w)
        , events(_events)
    {
        for (lint wi: w) {
            ws.insert(wi);
        }
    }

    void solve() {
        for (Event ev: events) {
            (this->*eventHandlers[ev.type])(ev);
        }
    }

    void handleSimulation(Event ev) {
        multiset<lint> removed;
        int steps = 0;
        lint s = ev.s;
        while (!ws.empty() && s < ev.k) {
            auto it = ws.lower_bound(s); // iterator to smallest wi such that s <= wi
            if (it == ws.begin()) {
                break;  // no fish smaller than s
            }
            --it;
            s += *it;
            removed.insert(*it);
            ws.erase(it);
            steps++;
        }

        if (s >= ev.k) {
           cout << steps << endl;
        } else {
            cout << -1 << endl;
        }

        for (lint wi: removed) {
            ws.insert(wi);
        }
    }

    void handleAdd(Event ev) {
        ws.insert(ev.w);
    }

    void handleRemove(Event ev) {
        ws.erase(ev.w);
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    vector<lint> w;
    vector<Event> events;
    read_data(w, events);
    Mlotek m(w, events);
    m.solve();
    return 0;
}