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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN = 3e5+5;

ll n,q,w,t,s,k,r,last,cnt;
map<ll,ll> a;

int main(){
	scanf("%lld", &n);
	for(int i = 0; i < n; i++){
		scanf("%lld", &w);
		a[w]++;
	}
	scanf("%lld", &q);
	while(q--){
		scanf("%lld", &t);
		if(t == 1){
			scanf("%lld%lld", &s, &k);
			r = 0,last = -1,cnt = 0;
			map<ll,ll> wziete;
			while(s < k){
				auto x = a.lower_bound(s);
				if(x == a.begin())	break;
				x = prev(x);
				while(wziete[(*x).first] == (*x).second){
					if(x == a.begin())	break;
					x = prev(x);
				}
				if((*x).first >= s || wziete[(*x).first] == (*x).second)	break;
				s += (*x).first;
				wziete[(*x).first]++;
				r++;
			}
			if(s < k)	printf("-1\n");
			else			printf("%lld\n", r);
		}else if(t == 2){
			scanf("%lld", &w);
			a[w]++;
		}else{
			scanf("%lld", &w);
			if(a.count(w) == 1)	a.erase(w);
			else						a[w]--;
		}
	}
}