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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using LL = long long;

struct Sprat
{
	LL w;
	bool active;
	int query;
	int next;
};

struct Query
{
	int type;
	LL a;
	LL b;
};

int main()
{
	int n, q, qt;
	LL w, qa, qb;

	cin >> n;

	vector<Sprat> sprats(n);

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

		sprats[i] = { w, true, -1, -1 };
	}

	cin >> q;

	vector<Query> queries(q);

	for (int i = 0; i < q; ++i)
	{
		cin >> qt;

		if (qt == 1)
		{
			cin >> qa >> qb;

			queries[i] = { qt, qa, qb };
		}
		else if (qt == 2)
		{
			cin >> qa;

			queries[i] = { qt, qa, -1 };

			Sprat sprat = { qa, false, -1, -1 };
			sprats.push_back(sprat);
		}
		else if (qt == 3)
		{
			cin >> qa;

			queries[i] = { qt, qa, -1 };
		}
	}

	sort(sprats.begin(), sprats.end(), [](const Sprat& a, const Sprat& b) { return a.w < b.w; });

	for (int i = 0; i < q; ++i)
	{
		if (queries[i].type == 1)
		{
			LL s = queries[i].a;
			LL k = queries[i].b;
			int res = 0;

			while (true)
			{
				if (s >= k)
				{
					cout << res << endl;
					break;
				}

				int low = 0;
				int high = sprats.size();

				while (low < high)
				{
					int mid = (low + high) / 2;

					if (sprats[mid].w >= s)
						high = mid;
					else
						low = mid + 1;
				}

				int org = --high;
				int ind = org;

				while (ind >= 0)
				{
					if (sprats[ind].query == i)
						ind = sprats[ind].next;
					else if (!sprats[ind].active)
						--ind;
					else
						break;
				}

				if (ind < 0)
				{
					cout << "-1" << endl;
					break;
				}
				
				s += sprats[ind].w;

				sprats[org].query = i;
				sprats[org].next = ind - 1;

				++res;
			}
		}
		else if (queries[i].type == 2)
		{
			int low = 0;
			int high = sprats.size();

			while (low < high)
			{
				int mid = (low + high) / 2;

				if (sprats[mid].w > queries[i].a)
					high = mid;
				else
					low = mid + 1;
			}

			--high;

			while (sprats[high].active)
				--high;

			sprats[high].active = true;
		}
		else if (queries[i].type == 3)
		{
			int low = 0;
			int high = sprats.size();

			while (low < high)
			{
				int mid = (low + high) / 2;

				if (sprats[mid].w > queries[i].a)
					high = mid;
				else
					low = mid + 1;
			}

			--high;

			while (!sprats[high].active)
				--high;

			sprats[high].active = false;
		}
	}

	return 0;
}