Passengers
InterviewTime limit1sMemory limit1024 MB
Given each request's row and earliest time, find the least total time for the attendant to serve all and return to row 1.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Every weekend a plane flies from Bitland to Vilnius. The passengers on this plane are very demanding and constantly trouble the crew: they keep asking for tea, a pillow, and so on.
While the crew tries to satisfy every request, the plane sometimes has to be kept circling above Vilnius before it can land! Naturally, the Bitland airline dislikes this, so from now on it asks its passengers to submit in advance a list of what they will request and when.
Given this list, find the minimum amount of time the flight attendant needs to fulfill all requests, assuming she plans her time optimally.
You also know that:
- Moving between two adjacent rows takes 1 minute.
- The flight attendant fulfills a request very quickly, so fulfilling a request is assumed to take no time (0 minutes).
- The flight attendant starts the flight standing at the first row.
- The flight attendant must finish the flight standing at the first row.
Input
The first line contains the number of requests .
Each of the next lines contains two integers and describing one passenger request. Here is the number of the row where the passenger sits, and is the earliest time at which the -th request is submitted (it may be fulfilled at that time or at any later time).
Output
Print a single integer — the minimum number of minutes needed for the flight attendant to fulfill all requests and return to the first row.