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
#include <cmath>
#include <iostream>
#include <cstdio>
#include <string>
#include <list>
#include <set>
#include <vector>
#include <forward_list>

using namespace std;
#define LONG long long

class Fish {
public:
   LONG mass;
   LONG expectedMass;
   LONG massDiff;
   long fishEaten;

   Fish() : mass(0LL), expectedMass(0LL), fishEaten(0L)
   {
      massDiff = 0LL;
   }

   bool Eat(LONG m)
   {
      mass += m;
      ++fishEaten;
      if (mass < expectedMass)
         return false;
      return true;
   }

   bool Full()
   {
      if (mass < expectedMass)
         return false;
      return true;
   }
};

class Event {
public:
   int evType;
   LONG mass;
   LONG param;

   Event() : evType(-1), mass(0LL), param(0LL) {}
};

class Fishbin {
public:
   multiset<LONG>::iterator fitLeft;
   multiset<LONG>::iterator fitEat;
   multiset<LONG>::iterator fitRight;

   Fishbin() {}
   Fishbin(multiset<LONG>::iterator itL, multiset<LONG>::iterator itR)
   {
      fitLeft = itL;
      fitEat = itR;
      fitRight = itR;
   }

};

multiset<LONG> setSprats;
vector<Event>  vecEvents;
forward_list<Fishbin> stackNets;

//list<long>     lstRes;
LONG sumSprats = 0LL;

long sprats = 0L;
LONG mass   = 0LL;
long events = 0L;
int  eventType = 0;
long nonSharkEvents = 0L;

Fish shark;

void EatSprats()
{
   bool bNew = false;
   multiset<LONG>::iterator fitMax = setSprats.begin();

   while (!stackNets.empty())
   {
      Fishbin& fb = stackNets.front();
      bNew = false;

      if (fitMax != setSprats.begin())
         fb.fitRight = fitMax;

      if (fb.fitRight == setSprats.end())
      {
         do
         {
            --fb.fitEat;
            if (shark.Eat(*fb.fitEat))
               return;
         } while (fb.fitEat != fb.fitLeft);
      }
      else // further right
      {
         do
         {
            --fb.fitEat;
            if (shark.Eat(*fb.fitEat))
               return;
            if (shark.mass > *fb.fitRight)
            {
               Fishbin newFb(fb.fitRight, setSprats.upper_bound(shark.mass));
               fitMax = newFb.fitRight;

               if (fb.fitEat == fb.fitLeft)
                  stackNets.pop_front();

               stackNets.push_front(newFb);
               bNew = true;
               break;
            }
         } while (fb.fitEat != fb.fitLeft);
      }

      if (!bNew)
         stackNets.pop_front();
   }
}

void StartEating()
{
   if (sumSprats == 0LL)
      return;

   Fishbin mouth;

   mouth.fitRight = setSprats.upper_bound(shark.mass);
   if (mouth.fitRight == setSprats.begin())
      return;
   mouth.fitLeft = setSprats.begin();
   mouth.fitEat = mouth.fitRight;

   stackNets.push_front(mouth);

   EatSprats();
}

void ProcessSprat(vector<Event>::const_iterator acit)
{
   if (acit->evType == 2) // new sprat
   {
      setSprats.insert(acit->mass);
      sumSprats += acit->mass;
   }
   else
   {
      setSprats.erase(setSprats.find(acit->mass));
      sumSprats -= acit->mass;
   }
}

long ProcessShark(vector<Event>::const_iterator acit)
{
   shark.mass = acit->mass;
   shark.expectedMass = acit->param;

   shark.fishEaten = 0L;
   stackNets.clear();

   if (shark.expectedMass <= shark.mass)
      return 0L;

   shark.massDiff = shark.expectedMass - shark.mass;

   if (shark.massDiff > sumSprats)
      return -1L;

   StartEating();

   if (shark.mass < shark.expectedMass)
      return -1L;
   else
      return shark.fishEaten;
}

void ReadData()
{
   cin >> sprats;

   while (sprats > 0L)
   {
      scanf("%lld", &mass);
      setSprats.insert(mass);
      sumSprats += mass;
      --sprats;
   }

   cin >> events;
   vecEvents.reserve(events);

   Event ev;

   while (events > 0L)
   {
      scanf("%d %lld", &ev.evType, &ev.mass);
      if (ev.evType == 1)
        scanf("%lld", &ev.param);
      else
         ++nonSharkEvents;

      vecEvents.push_back(ev);
      --events;
   }

   for (vector<Event>::const_iterator cit = vecEvents.begin(); cit != vecEvents.end(); ++cit)
   {
      if (cit->evType > 1) // no Shark
      {
         ProcessSprat(cit);
      }
      else
      {
         printf("%ld\n", ProcessShark(cit));
      }
   }
}

int main(int argc, char* argv[])
{
   ReadData();

   return 0;
}