atcoder#ABC193E. [ABC193E] Oversleeping
[ABC193E] Oversleeping
Score : points
Problem Statement
A train goes back and forth between Town and Town . It departs Town at time and then repeats the following:
- goes to Town , taking seconds;
- stops at Town for seconds;
- goes to Town , taking seconds;
- stops at Town for seconds.
More formally, these intervals of time are half-open, that is, for each :
- at time such that , the train is going to Town ;
- at time such that , the train is stopping at Town ;
- at time such that , the train is going to Town ;
- at time such that , the train is stopping at Town .
Takahashi is thinking of taking this train to depart Town at time and getting off at Town . After the departure, he will repeat the following:
- be asleep for seconds;
- be awake for seconds.
Again, these intervals of time are half-open, that is, for each :
- at time such that , Takahashi is asleep;
- at time such that , Takahashi is awake.
He can get off the train at Town if it is stopping at Town and he is awake. Determine whether he can get off at Town . If he can, find the earliest possible time to do so. Under the constraints of this problem, it can be proved that the earliest time is an integer.
You are given cases. Solve each of them.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Each case is in the following format:
Output
Print lines.
The -th line should contain the answer to .
If there exists a time such that Takahashi can get off the train at Town , the line should contain the earliest such time; otherwise, the line should contain infinity
.
3
5 2 7 6
1 1 3 1
999999999 1 1000000000 1
20
infinity
1000000000999999999
Let denote the interval .
In the first case, the train stops at Town during , and Takahashi is awake during , so he can get off at time at the earliest.