Airplane Passengers
InterviewTime limit1sMemory limit1024 MB
Given requests at row a made no earlier than minute b, find the minimum minutes for an attendant starting at row 1 to reach every request row on time.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Greedy, Math
- Solved
- No attempts yet
Problem
Every weekend a plane flies from Bitland to Vilnius, and its passengers are extremely demanding: they keep asking the crew for tea, a pillow, and so on. A single flight attendant must fulfill every request.
The rows of the cabin are numbered . The attendant starts the flight (at minute ) standing at row . Walking from one row to an adjacent row takes exactly minute, and fulfilling a request itself takes no time (it is instantaneous).
Each request is given by two integers : the passenger sits in row , and the request is made no earlier than minute . Time is measured in minutes, starting from minute at the beginning of the flight. A request may be fulfilled at minute or at any later minute, but never before minute . To fulfill a request, the attendant must be standing in that passenger's row at a time of at least .
The attendant may finish the flight standing at any row. Planning her movements optimally, find the minimum number of minutes needed to fulfill every request.
Input
The first line contains the number of requests .
Each of the next lines contains two integers and separated by a space, describing one request. Here is the number of the row where the passenger sits, and is the earliest time at which the -th request may appear (it may be fulfilled later than this).
Output
Print a single integer: the minimum number of minutes the attendant needs in order to fulfill every request.