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
#include <iostream>
#include <queue>
#include <limits>

using namespace std;

typedef long long ll;
typedef priority_queue<ll> pq;

pq operator+(pq a, pq b)
{
	while(!b.empty())
	{
		a.push(b.top());
		b.pop();
	}
	return a;
}

int eat(pq staw, ll ryba, ll glod)
{
	if(staw.empty()) return -1;
	pq t;
	int eat = 0;
	while(ryba < glod)
	{
		if (staw.empty()) {return -1;}
		while(staw.top() >= ryba)
		{
			if (staw.empty()) {return -1;}
			t.push(staw.top());
			staw.pop();
		}
		ryba += staw.top();
		staw.pop();
		eat++;
		staw = t + staw;
		while(!t.empty()) {t.pop();}
	}
	return eat;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	
	int n, q, typ; //szprotek, pytan, rodzaj pytania
	cin >> n;
	pq w, temp;
	ll a, szczupak, koniec;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		w.push(a);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		cin >> typ;
		if (typ == 1)
		{
			cin >> szczupak >> koniec;
			cout << eat(w, szczupak, koniec) << '\n';
		}
		else
		{
			cin >> a;
			if (typ == 2)
			{
				w.push(a);
			}
			else
			{
				while(w.top() != a)
				{
					temp.push(w.top());
					w.pop();
				}
				w.pop();
				w = w + temp;
				while(!temp.empty()) {temp.pop();}
			}
		}
	}
	return 0;
}