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
/*
 *  Copyright (C) 2019  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <set>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <numeric>
using namespace std;

struct Shark {
	long long int weight;
	long long int target;

	Shark(long long int w, long long int t): weight(w), target(t) {}
};


unordered_set<multiset<long long int>::const_pointer> used;

void print_eaten(vector<Shark>& attacks, multiset<long long int>& fishes) {
	// no eating if there is no fish
	if (fishes.empty()) {
		for (unsigned int i = 0; i < attacks.size(); ++i) {
			cout << -1 << endl;
		}
		return;
	}

	vector<int> index(attacks.size());
	iota(begin(index), end(index), 0);

	// index sort attacks in decreasing order
	sort(begin(index), end(index),
		[&attacks](int a, int b) {
			if (attacks[a].weight == attacks[b].weight) return attacks[a].target > attacks[b].target;
			else return attacks[a].weight > attacks[b].weight;
		}
	);

	vector<int> eaten(attacks.size(), 0);

	//last = begin(attacks)->weight;
	for (auto i: index) {
		auto shark = attacks[i];
//		if (possible.size() > 0) {
//			auto it = lower_bound(begin(possible), end(possible), shark.target);
//			if (it != possible.end()) {
//				eaten.emplace_back();
//			}
//		}

		int count = 0;
		used.clear();

//		cout << "s " << shark.weight << " " << shark.target << endl;

		for (auto weight = shark.weight; weight < shark.target;) {
//			cout << "weight = " << weight << endl;

			// find largest fish the shark can eat
			auto it = prev(fishes.upper_bound(weight - 1));
			for (; it != fishes.begin() && used.count(&(*it)) > 0; --it) {}
			if (it == fishes.begin() && used.count(&(*it)) > 0) {
				it = fishes.end();
			}
			// all accessible fishes eaten
			if (it == fishes.end()) {
//				cout << "no fish" << endl;
				count = -1;
				break;
			}

			// eat the fish
			++count;
			weight += *it;
//			cout << "eating " << *it << " at step " << count << endl;
			// possible.emplace_back(weight);
			if (weight < shark.target) {
				used.insert(&(*it));
			}
		}

		eaten[i] = count;
//		cout << "target reached in " << count << endl;

	}

	for (auto value : eaten) {
		cout << value << endl;
	}
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, event;
	cin >> n;

	long long int weight, target;
	multiset<long long int> fishes;

	for (int i = 0; i < n; ++i) {
		cin >> weight;
		fishes.emplace(weight);
	}

	cin >> n;
	vector<Shark> attacks;
	attacks.reserve(n);
	used.reserve(n);

	for (int i = 0; i < n; ++i) {
		cin >> event;
		// shark attack
		if (event == 1) {
			cin >> weight >> target;
			attacks.emplace_back(weight, target);

		// analyse and reset the attacks before changes in the pond
		} else {
			if (attacks.size() > 0) {
				print_eaten(attacks, fishes);
			}
			attacks.clear();

			cin >> weight;
			if (event == 2) {
				fishes.insert(weight);
			} else {
				fishes.erase(weight);
			}
		}
	}

	// analyse the remaining attacks
	if (attacks.size() > 0) {
		print_eaten(attacks, fishes);
	}

	return 0;
}