#P7748. Pass the Box
Pass the Box
Pass the Box
There are 8 people sitting in a circle. Initially, one of them is holding a box which is set to explode exactly 210 seconds after the contest begins.
The host will ask questions one by one to the current box holder. For each question:
- If the box holder either skips the question or answers it incorrectly, the host will simply move on to the next question and the box remains with the same person.
- If the answer is correct, the box is immediately passed to the person on the left-hand side (i.e. the next person in the circle). For the purpose of this problem, assume the people are numbered from 1 to 8 in order and the left-hand neighbor of person i is ((i % 8) + 1).
You are given the initial box holder's number and the total number of questions. Then for each question, you are given the time taken to answer that question and whether the answer was correct (represented as 1 for a correct answer and 0 for an incorrect answer).
The simulation continues until the cumulative time reaches or exceeds 210 seconds. Note that if a question’s time would cause the total time to exceed 210 seconds, then that question is not fully processed (i.e. the answer is not considered) and the box remains with the current holder. However, if the cumulative time becomes exactly 210 seconds after processing a question, then the answer is processed and the box may be passed accordingly before it explodes.
The data guarantees that at the moment of the explosion the box is in someone's possession and not in transit, and that not all questions are used.
Your task is to determine the number of the person holding the box when it explodes.
All time values, including the question times, are in seconds. The passing of the box between people is instantaneous and the time between questions is negligible.
Mathematically, if we let \( T \) be the cumulative time, then the simulation for each question with time \( t_i \) is as follows:
\[ T_{new} = T_{old} + t_i, \] \[ \text{if } T_{old} + t_i > 210, \text{ explosion occurs and the answer is not processed}; \] \[ \text{if } T_{old} + t_i = 210, \text{ process the answer and then the explosion happens}; \] \[ \text{if the answer is correct, update the current holder } x \text{ to } ((x \mod 8) + 1). \]inputFormat
The first line contains two integers:
s
(1 ≤ s ≤ 8) — the initial box holder's number, andn
— the number of questions.
Then follow n
lines, each containing two integers:
t
(the time in seconds taken for that question), andr
(either 1 if the answer is correct or 0 if it is not).
The input guarantees that the explosion occurs while the box is held by someone, and that not all questions are processed.
outputFormat
Output a single integer representing the number of the person holding the box when it explodes.
sample
3 4
10 1
20 0
200 1
50 0
4
</p>