Problem D
Avoiding Airports

David is currently at the airport in country
Input
The first line of input contains two space-separated
integers
Each of the next
A flight might have the same departure and arrival country.
No two flights will have the same arrival time, or have the same departure time. In addition, no flight will have the same arrival time as the departure time of another flight. Finally, it is guaranteed that there will always be a way for David to arrive at his destination.
Output
Print, on a single line, the minimum sum of frustration.
Examples
In the first sample, it is optimal to take this sequence of flights:
-
Flight
. Goes from airport to airport , departing at time , arriving at time . -
Flight
. Goes from airport to airport , departing at time , arriving at time . -
Flight
. Goes from airport to airport , departing at time , arriving at time . -
Flight
. Goes from airport to airport , deparing at time , arriving at time .
The frustration for each flight is
Note that there is an itinerary that gets David to his destination faster. However, that itinerary has a higher total frustration.
Sample Input 1 | Sample Output 1 |
---|---|
5 8 1 2 1 10 2 4 11 16 2 1 9 12 3 5 28 100 1 2 3 8 4 3 20 21 1 3 13 27 3 5 23 24 |
12 |
Sample Input 2 | Sample Output 2 |
---|---|
3 5 1 1 10 20 1 2 30 40 1 2 50 60 1 2 70 80 2 3 90 95 |
1900 |