Flip Flow
InterviewTime limit1sMemory limit512 MB
An hourglass starts with all sand at the bottom, is flipped at given times, and you report how long after time t the sand finishes returning to the bottom.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Math, Prefix sum
- Solved
- No attempts yet
Problem
For over 600 years, the hourglass has been a well-known timekeeping instrument. An hourglass consists of two glass flasks arranged one above the other, connected by a narrow channel. Inside the hourglass there is sand which slowly flows from the upper to the lower flask. Hourglasses are typically symmetrical so that the sand always takes the same amount of time to run through, regardless of orientation. For the purposes of this problem, we also assume that the flow rate of the sand is a known constant and does not depend on the amount or distribution of sand in the upper half.
Your friend Helen was bored and has been playing around with her hourglass. At time , all the sand was in the lower half. Helen flipped the hourglass over several times and recorded all the moments at which she did so. How many seconds does she need to wait from the current time until all the sand is back in the lower half?
Input
The input consists of:
- One line with three integers , and , where
- () is the current time;
- () is the amount of sand in the hourglass, in grams;
- () is the number of times the hourglass was flipped.
- One line with integers (), the times at which the hourglass was flipped.
All times are given in seconds. You may assume that the sand flows from the top to the bottom at a constant rate of gram per second.
Output
Output the time in seconds needed for the hourglass to run out starting from time .