When you go out from the toilet to go back to class you discover that a group of velociraptors has entered to the classroms and has devoured your classmates. The corridor where you are is closed: running away is impossible. Velociraptors, inside the classroms digesting, will go out at any moment to finish with you. Oh, well! It is known that this kind of things happen sometimes.
The corridor of your high school is represented by a segment of the real line from to , with doors of classroms, placed over the points of the line. The toilet where you are going out from is placed at the point with and even . You as well as the velociraptors take 1 second to cover a distance unit over the line (velociraptors are already satisfied and they are not going to run for a miserable desert).
You are asked to, assuming that you know which velociraptors will go out from the classroms to devour you and the moments of time that they will do it, and also assuming that these ones will head for you (wherever you are) as soon as they go out, say how many seconds you can extend your (brief but intense) life time making the right movements.
We consider that will be very useful to think in space-time diagrams as the one on the right, where it is illustrated a possible situaton for and , where 3 velociraptors go out from the classroms placed in the points 2, 4 and 14 at the moments 6, 10 and 8 respectively. The correct answer to this case is .
A test data contains various cases. Each case starts with three naturals , and , with , and , where and are as it is describe in the wording and is the number of velociraptors. The next lines of the input contain a pair of numbers , , where is the classroom that has devoured the -th velociraptor and is the moment of time that it will go out for its desert. It is fulfilled that and for any , that and are even, and that all the are different.
For each case, your program must print in a line the time that you can extend your life. As times and classrooms are even numbers it is fulfilled that the answer will always be an integer.
Test1:
Test data with no more than 20 cases with and where the appear sorted (as in the instance 1).
Test2:
Test data with no more than 20 cases with and (as in the instances 2 and 3).
Test3:
Test data with no more than 20 cases of and (as in the instance 4).
Author: Unknown
Translator: Carlos Molina
Generation: 2026-01-25T12:16:28.387Z
© Jutge.org, 2006–2026.
https://jutge.org