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
#include <bits/stdc++.h>

typedef long long ll;

int P, K, M;

std::multiset<ll>::iterator find(const std::multiset<ll>& s, const ll x) {
  std::multiset<ll>::iterator t = s.lower_bound(x);

  if (t == s.end())
    return std::prev(s.end());
  
  if (t == s.begin())
    return s.end();
  
  return --t;
}

int main() {
  int n; std::cin >> n;
  ll x;
  std::multiset<ll> s;
  
  for (int i = 0; i < n; ++i) {
    std::cin >> x;
    s.insert(x);
  }

  int q; std::cin >> q;
  int k;
  ll st, e;
  std::vector<ll> used;
  while (q--) {
    std::cin >> k;
    if (k == 1) {
      std::cin >> st >> e;
      while (st < e && !s.empty()) {
        std::multiset<ll>::iterator i = find(s, st);
        if (i == s.end()) break;
        st += *i;
        used.push_back(*i);
        s.erase(i);
      }
      if (st < e)
        std::cout << -1 << std::endl;
      else
        std::cout << used.size() << std::endl;
      for (const auto& i : used)
        s.insert(i);
      used.clear();
    }
    else if (k == 2) {
      std::cin >> x;
      s.insert(x);
    }
    else if (k == 3) {
      std::cin >> x;
      s.erase(s.find(x));
    }
  }

  return 0;
}