Today is a special day since TT1103 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 TT1103 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.
TT1103 is a nice person, so he doesn't want to inconvenience people. As such, TT1103 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.
- 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 TT1103 can microwave his lunch?
The first line will contain three space-separated integers, , , and .
lines of input follow. The line will contain two space-separated integers, and .
A single integer specifying the minimal number of minutes after the start of lunch, showing when TT1103 can microwave his food.
Sample Input 1
2 2 5 0 5 3 1
Sample Output 1
Explanation for Sample Output 1
TT1103 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
Explanation for Sample Output 2
TT1103 cannot microwave his lunch before person 1 or person 2, since others will need to wait for him to finish, but if TT1103 microwaves his food immediately after person 2 is done, TT1103 will be done just as person 3 enters the line.
Sample Input 3
1 2 3 0 5 1 3
Sample Output 3
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, TT1103 cannot start microwaving his food until 8 minutes after lunch starts.