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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define pb pop_back
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)

#define Sim template<class n
Sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
Sim> auto operator << (ostream &p, n y) -> 
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
Sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " = ", (x), " "
#define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " "
const int M = 1024 * 512;
const int inf = 1e9;
const LL INF = 1e18L;

struct query
{
	LL b, c;
	int a;
};

struct node
{
	LL sum;
	int ile, flag;
};

int n, q;
LL T[M];
multiset<LL> els;
map<LL, int> curr, mx, pos;
vector<query> Q;
node D[2 * M];

void add(LL x)
{
	curr[x]++;
	maxi(mx[x], curr[x]);
}

void rem(LL x)
{
	curr[x]--;
}

void popraw(int v)
{
	D[v].sum = 0;
	D[v].ile = 0;
	if (!D[2 * v].flag)
	{
		D[v].sum += D[2 * v].sum;
		D[v].ile += D[2 * v].ile;
	}
	if (!D[2 * v + 1].flag)
	{
		D[v].sum += D[2 * v + 1].sum;
		D[v].ile += D[2 * v + 1].ile;
	}
}

inline LL gets(int v)
{
	if (D[v].flag)
	{
		return 0;
	}
	return D[v].sum;
}

inline LL geti(int v)
{
	if (D[v].flag)
	{
		return 0;
	}
	return D[v].ile;
}

void setflag(int v, int vp, int vk, int p, int k, int f)
{
	if (vp > k || vk < p)
	{
		return;
	}
	if (vp >= p && vk <= k)
	{
		D[v].flag = f;
		return;
	}
	int mid = (vp + vk) / 2;
	setflag(2 * v, vp, mid, p, k, f);
	setflag(2 * v + 1, mid + 1, vk, p, k, f);
	popraw(v);
}

void addT(int v, LL s, int i)
{
	v += M;
	D[v].sum += s;
	D[v].ile += i;
	while (v > 1)
	{
		v /= 2;
		popraw(v);
	}
}

pair<LL, int> getstart(int v, LL s)
{
	if (gets(v) < s)
	{
		return MP(gets(v), -inf);
	}
	if (v >= M)
	{
		return MP(gets(v), v - M);
	}
	if (gets(2 * v + 1) >= s)
	{
		return getstart(2 * v + 1, s);
	}
	return getstart(2 * v, s - gets(2 * v + 1));
}

pair<LL, int> getint(int v, int vp, int vk, int end, LL s)
{
	if (vp > end || D[v].flag)
	{
		return MP(0, -inf);
	}
	if (vk <= end)
	{
		return getstart(v, s);
	}
	int mid = (vp + vk) / 2;
	pair<LL, int> x = getint(2 * v + 1, mid + 1, vk, end, s);
	//debug(v, vp, vk, x, end, s);
	if (x.s >= 0)
	{
		return x;
	}
	s -= x.f;
	pair<LL, int> ret = getint(2 * v, vp, mid, end, s);
	return MP(ret.f + x.f, ret.s);
}

template<class A, class B>
pair<A, B> operator +(pair<A, B> x, pair<A, B> y)
{
	return MP(x.f + y.f, x.s + y.s);
}

pair<LL, int> getsum(int v, int vp, int vk, int p, int k)
{
	if (vp > k || vk < p || D[v].flag)
	{
		return MP(0, 0);
	}
	if (vp >= p && vk <= k)
	{
		return MP(gets(v), geti(v));
	}
	int mid = (vp + vk) / 2;
	return getsum(2 * v, vp, mid, p, k) + getsum(2 * v + 1, mid + 1, vk, p, k);
}

void add2(LL x)
{
	els.insert(x);
	int ind = pos[x] + curr[x];
	curr[x]++;
	//debug(x, ind);
	addT(ind, x, 1);
}

void rem2(LL x)
{
	els.erase(els.find(x));
	int ind = pos[x] + curr[x] - 1;
	curr[x]--;
	//debug(x, ind);
	addT(ind, -x, -1);
}

void on(VPII flags)
{
	for (auto x : flags)
	{
		setflag(1, 0, M - 1, x.f, x.s, 0);
	}
}

int licz = 0, qs = 0;

int solve(LL a, LL b)
{
	qs++;
	//debug(a, b, els);
	int res = 0;
	VPII flags;
	while (a < b)
	{
		licz++;
		auto it = els.lower_bound(a);
		int end = pos[*it] - 1;
		//debug(a, b, *it, end, min(b, (*it) + 1) - a);
		pair<LL, int> start = getint(1, 0, M - 1, end, min(b, (*it) + 1) - a);
		//debug(start);
		if (start.s < 0)
		{
			on(flags);
			return -1;
		}
		pair<LL, int> s = getsum(1, 0, M - 1, start.s, end);
		//debug(start, s, *it, end, min(b, (*it)) - a);
		a += s.f;
		res += s.s;
		setflag(1, 0, M - 1, start.s, end, 1);
		flags.PB(MP(start.s, end));
	}
	on(flags);
	return res;
}

const LL mod = 1e9 + 7;

int32_t main()
{
	//ios_base::sync_with_stdio(0);
	//cin.tie(NULL);
	scanf("%d", &n);
	//n = 3e5;
	LL ss = 0;
	FOR(i, 1, n)
	{
		LL a;
		scanf("%lld", &a);
		//a = ran(1e12L);
		T[i] = a;
		ss += a;
		add(a);
	}
	sort(T + 1, T + n + 1);
	scanf("%d", &q);
	//q = 1e5;
	FOR(i, 1, q)
	{
		query x{0, 0, 0};
		scanf("%d%lld", &x.a, &x.b);
		//x.a = 1;
		//x.b = ran(1e12L);
		if (x.a == 1)
		{
			scanf("%lld", &x.c);
			//x.c = x.b + ss;
		}
		else if (x.a == 2)
		{
			add(x.b);
		}
		else
		{
			rem(x.b);
		}
		Q.PB(x);
	}
	curr.clear();
	int tmp = 0;
	for (auto el : mx)
	{
		pos[el.f] = tmp;
		tmp += el.s;
	}
	els.insert(INF);
	pos[INF] = M;
	FOR(i, 1, n)
	{
		add2(T[i]);
	}
	//debug(mx, pos);
	for (auto x : Q)
	{
		if (x.a == 2)
		{
			add2(x.b);
		}
		else if (x.a == 3)
		{
			rem2(x.b);
		}
		else
		{
			int result = solve(x.b, x.c);
			printf("%d\n", result);
		}
	}
	debug(qs, licz, (LD)licz / qs);
	return 0;
}