#P10711. Amusement Park Queue Boarding Simulation

    ID: 12743 Type: Default 1000ms 256MiB

Amusement Park Queue Boarding Simulation

Amusement Park Queue Boarding Simulation

An amusement park offers a sightseeing bus service at its entrance. Since each bus has a limited seating capacity, when a team arrives at the gate, they must decide whether to split up if the bus cannot accommodate all of them. Some teams are willing to split up while others are not.

The park manager, Snail Stuart, needs your help to process a sequence of operations on the queue. The operations are as follows:

  • join s w: A new team joins the queue. The team is described by two integers: s (the number of people in the team) and w (a flag indicating whether the team is willing to split up during boarding, where 1 means willing and 0 means not willing). The teams are numbered in the order in which their join operations occur (i.e. the first join gives team number 1, the second join gives team number 2, and so on).
  • leave i: The team with number i leaves the queue.
  • board b: A sightseeing bus with capacity b arrives. The boarding process works as follows:
    1. Start from the front of the queue.
    2. If the team at the front can fit entirely in the bus (i.e. team size ≤ remaining bus capacity), board the entire team and remove it from the queue.
    3. If the team cannot fit entirely and the team is willing to split (i.e. w = 1), then board as many people as possible from that team (i.e. the remaining capacity) and update the team’s size (the team remains in the queue with the remaining people).
    4. If the team cannot fit and is not willing to split (i.e. w = 0), then skip this team and move on to the next team in the queue.
    5. Repeat the above steps until the bus is full or no team that is willing to split can board partially.

For each board operation, output a single integer: the total number of people who boarded the bus in that operation.

inputFormat

The first line contains an integer n (the number of operations). Each of the following n lines contains an operation in one of the following formats:

  • join s w
  • leave i
  • board b

It is guaranteed that all numbers are positive integers, and the operations are given in the order they occur.

outputFormat

For each board operation, output a single line with an integer representing the number of people who boarded the bus during that operation.

sample

6
join 5 0
join 3 1
board 4
join 2 1
join 6 0
board 7
3

7

</p>