Airplane
InterviewTime limit1sMemory limit128 MB
Simulate passengers walking down a single aisle to their assigned row and taking 5 seconds to load luggage, blocked by others ahead, to find total boarding time.
- Level
Medium5 of 10
- Topics
- Simulation, Queue, Greedy
- Solved
- No attempts yet
Problem
Imagine an airplane with passengers boarding to find their seats, and a single aisle running along the rows.
Every passenger must sit in their seat. The boarding door is right in front of the first row of seats, and passengers enter the aisle one after another without any unnecessary stops.
A passenger walks down the aisle; advancing past one row to the next takes exactly 1 second (if another passenger is right ahead, they wait there). From the moment they reach their own row, they spend 5 seconds loading their luggage and then sit down.
Only one passenger can occupy a given aisle row at a time. Write a program that computes the time it takes for all passengers to be seated, i.e., for the airplane to be ready for takeoff.
Input
The first line contains the number of passengers N. (1 ≤ N ≤ 1000)
Each of the next N lines contains the row number R of the seat a passenger must sit in, one per line. (1 ≤ R ≤ 1000)
Passengers are numbered 1 to N and are given in that order. There is no limit on how many passengers sit in the same row.
Output
Print, on the first line, the time in seconds it takes for all passengers to be seated.
Hint
If a passenger heading to row 1 is right behind a passenger heading to row 4, the one going to row 1 can load their luggage and sit down first.