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
//Filip Wilkowski XIII LO Szczecin

#include <bits/stdc++.h>
using namespace std;

multiset<long long> staw;
multiset<long long>::iterator it;

void dodaj(long long w){
	staw.insert(w);
}

void usun(long long w){
	it = staw.find(w);
	staw.erase(it);
}

long long szukaj(long long s){
	if(staw.size() == 0)
		return -1;
	auto temp = staw.begin();
	if(*temp > s)
		return -1;
	it = staw.find(s);
	if(it == staw.end())
		it = staw.upper_bound(s);
	if(it == staw.begin())
		return -1;
	--it;
	return *it;
}

int szczupak(long long s, long long k){
	vector<int> deleted;
	long long x = szukaj(s);
	while(x != -1 && s < k){
		s += x;
		deleted.emplace_back(x);
		it = staw.find(x);
		staw.erase(it);
		x = szukaj(s);
	}
	if(s < k){
		for(long long w:deleted)
			dodaj(w);
		deleted.clear();
		return -1;
	} else{
		int y = (int)deleted.size();
		for(long long w:deleted)
			dodaj(w);
		deleted.clear();
		return y;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	for(int i = 0; i < n; ++i){
		long long w;
		cin >> w;
		dodaj(w);
	}

	int t;
	cin >> t;
	while(t--){
		int task;
		cin >> task;
		if(task == 1){
			long long s, k;
			cin >> s >> k;
			cout << szczupak(s, k) << '\n';
		}
		else if(task == 2){
			long long w;
			cin >> w;
			dodaj(w);
		} else{
			long long w;
			cin >> w;
			usun(w);
		}
	}
}