TLE '15 P2 - Microwaves
View as PDFToday is a special day since isn't going to buy lunch at a nearby restaurant! His parents ran out of money, so he had to bring a microwavable lunch today.
Pierre Elliott Trudeau High School is also famous for its faulty microwaves. There are many microwaves available, but only a few of them actually heat up food. Today,  
 microwaves are functional, and  needs to microwave his food for 
 
 minutes.
There are also  
 people in the cafetorium today who need to microwave their food, named 
 to 
. People must wait in a line in order to use the microwaves, and people are not allowed to cut in front of others. The 
 person will go to the back of the line 
 
 minutes after lunch starts, and needs to microwave food for 
 
 minutes.
Immediately after a person is done using a microwave, somebody else can use it. If there are microwaves available, the person at the front of the line can either wait, or leave the line and choose a microwave. Only the person at the front of the line can exit the line and choose a microwave. If no microwaves are available, then the person at the front of the line must wait. Each microwave can only serve 1 person, nobody can interrupt their turn at a microwave, and it is guaranteed that no two people will arrive at the exact same time.
 is a nice person, so he doesn't want to inconvenience people. As such,  will only microwave his food if at all times during  continuous minutes:
- The person at the front of the line waits 
minutes after reaching the front before going to an available microwave.
or - There is nobody in the line.
 
You are tasked with guiding people to an available microwave or telling them to wait. If you guide people in an optimal way, what is the minimal number of minutes after the start of lunch until can microwave his lunch?
Input Specification
The first line will contain three space-separated integers, , 
, and 
.
 lines of input follow. The 
 line will contain two space-separated integers, 
 and 
.
Output Specification
A single integer specifying the minimal number of minutes after the start of lunch, showing when can microwave his food.
Sample Input 1
2 2 5
0 5
3 1
Sample Output 1
4
Explanation for Sample Output 1
can microwave his lunch 4 minutes after lunch starts by taking his turn after the second person is done.
Sample Input 2
1 3 4
2 4
9 2
15 3
Sample Output 2
11
Explanation for Sample Output 2
cannot microwave his lunch before person 1 or person 2, since others will need to wait for him to finish, but if microwaves his food immediately after person 2 is done, will be done just as person 3 enters the line.
Sample Input 3
1 2 3
0 5
1 3
Sample Output 3
8
Explanation for Sample Output 3
Person 2 cannot microwave his food before person 1 is done, so he will not start until 5 minutes after lunch starts. Thus, cannot start microwaving his food until 8 minutes after lunch starts.
Comments
Don't you just hate it when your school only has 100 operational microwaves?
This comment is hidden due to too much negative feedback. Show it anyway.
ZQFMGB12 gave me some of his cold leftovers.