Freight
Time limit1sMemory limit256 MB
Schedule n freight trains over a single track with travel time s and one-minute departure gaps so the last train returns to Upper Bytown as early as possible.
- Level
Medium7 of 10
- Topics
- Greedy, Simulation
- Solved
- No attempts yet
Problem
The train stations of Upper Bytown and Lower Bytown are joined by a single track. A train needs minutes to travel between them, in either direction. Trains leaving the same station must depart at least one minute apart. At every moment, all trains on the track have to move in the same direction. A train may depart from a station at the exact minute another train arrives there.
According to the timetable, freight trains bound for Lower Bytown pass through Upper Bytown. Train reaches Upper Bytown at minute and cannot depart before that. Each train runs to Lower Bytown, takes on goods there, and returns to Upper Bytown. Loading the goods takes no time.
Find the earliest minute at which the last train can be back in Upper Bytown.
Input
The first line contains two integers and separated by a single space, the number of trains and the one-way travel time (, ). The second line contains integers separated by single spaces, the arrival times of the trains at the Upper Bytown station ().
Output
Print one line with a single integer: the earliest minute at which the last train is back in Upper Bytown.
Hint
In the first example the minimum is reached when the trains leave Upper Bytown at minutes 1, 9 and 11, and leave Lower Bytown at minutes 5, 15 and 16.