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
#include <iostream>
#include <vector>
#include <algorithm>

using ll = long long;




int answer(std::vector<ll> const &ws, std::vector<ll> const &cs, const ll s, const ll k)
{
  int eaten = 0;
  ll weight = s;
  ll wdiff = k - weight;
  // stack keeps (max_pos (excluded), num_eaten)
  std::vector<std::pair<ll, ll>> stack = { {0, 0} };

  while (wdiff > 0) {
    auto next_max = std::lower_bound(std::cbegin(ws), std::cend(ws), weight);
    if (next_max == std::begin(ws)) {
      // we can't eat any other fish
      return -1;
    }

    auto gain = std::prev(next_max);
    // how much we need to either achieve k or reach next MAX
    ll need = wdiff - 1;  // let's hope this (-1) with not break everything
    if (next_max != std::cend(ws)) {
      need = std::min(*next_max - weight, need);
    }

    // let's eat this fish
    ll dist = std::distance(std::cbegin(ws), next_max);
    if (stack.back().first + 1 == dist) {
      // by eating this fish we will use whole range
      auto [pm, pv] = stack.back();
      stack.back() = { dist, pv + 1 };
    } else {
      stack.push_back({ dist, 1 });
    }
    weight += *gain;
    wdiff  -= *gain;
    need   -= *gain;
    eaten  += 1;

    // let's try achieve next MAX
    while (need >= 0 && stack.size() >= 2) {
      auto [cpos, cnum] = stack[stack.size() - 1];
      auto [mpos, mnum] = stack[stack.size() - 2];
      // optimization: use `cs` to check if we are going to eat whole range
      ll base = cs[cpos - 1];
      auto lim = std::cbegin(cs) + cpos - cnum + 1;
      auto rng = std::upper_bound(std::cbegin(cs) + mpos + 1, lim, need,
                                  [=](auto &&v, auto &&t) { return -v <= -(base - t); });

      ll range = std::distance(rng, lim);
      ll gained = base - *std::prev(rng);
      need   -= gained;
      weight += gained;
      wdiff  -= gained;
      eaten  += range;

      if (rng == std::cbegin(cs) + mpos + 1) {
        // we need to eat whole range
        // merge both ranges
        stack.pop_back();
        stack.pop_back();
        stack.push_back({ cpos, mnum + cpos - mpos });
      } else {
        // update max on stack (we have enough)
        stack.back().second += range;
      }
    }

    if (need >= 0 && wdiff > 0) {
      return -1;  // we can't advance MAX
    }

    if (wdiff <= 0) {
      break;
    }
  }

  return eaten;
}




int main()
{
  std::ios::sync_with_stdio(false);

  int n;
  std::cin >> n;

  std::vector<ll> ws(n);
  std::vector<ll> cs(n + 1, 0);

  for (int i = 0; i < n; ++i) {
    std::cin >> ws[i];
  }

  std::sort(std::begin(ws), std::end(ws));

  for (int i = 0; i < n; ++i) {
    cs[i + 1] = ws[i] + cs[i];
  }
  
  int q;
  std::cin >> q;

  while (q--) {
    int t;
    std::cin >> t;
    if (t == 1) {
      ll s, k;
      std::cin >> s >> k;
      std::cout << answer(ws, cs, s, k) << std::endl;
    } else if (t == 2) {
      // naive version...
      ll w;
      std::cin >> w;
      auto hint = std::upper_bound(std::cbegin(ws), std::cend(ws), w);
      auto it = ws.insert(hint, w);
      ll i = std::distance(std::begin(ws), it);
      cs.insert(std::cbegin(cs) + i + 1, 0);
      cs[i + 1] = cs[i];
      for (++i; i < cs.size(); ++i) {
        cs[i] += w;
      }
    } else if (t == 3) {
      // naive version...
      ll w;
      std::cin >> w;
      auto hint = std::prev(std::upper_bound(std::cbegin(ws), std::cend(ws), w));
      auto it = ws.erase(hint);
      ll i = std::distance(std::begin(ws), it);
      cs.erase(std::cbegin(cs) + i + 1);
      for (++i; i < cs.size(); ++i) {
        cs[i] -= w;
      }
    }
  }

  return 0;
}