Train Scheduling
Time limit2sMemory limit1024 MB
Schedule N trains from two stations on one shared track so opposite-direction trains never overlap, minimizing the total delay past each train's earliest departure.
- Level
Hard8 of 10
- Topics
- Sorting, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Bessie has taken a new job as a train dispatcher. There are two stations, and . Because of budget limits, only one track connects them. If a train departs a station at time , it arrives at the other station at time ().
There are trains () whose departure times must be scheduled. The th train must leave station at time or later (, ).
Trains going in opposite directions may not be on the track at the same time, because they would crash. Many trains may travel in the same direction at the same time. Assume trains have negligible size.
Input
The first line contains and . Each of the next lines contains the station and the time for the th train.
Output
Print the minimum possible total delay over all valid schedules. If train departs at time , the total delay is .