#P1897. Elevator Time Calculation

    ID: 15180 Type: Default 1000ms 256MiB

Elevator Time Calculation

Elevator Time Calculation

Little W has noticed that he rides the elevator frequently, partly in hopes of encountering his idol. While chatting with his idol in the elevator, he also pays attention to the floor indicator. He notices that:

  • Moving up one floor takes \(6\) seconds.
  • Moving down one floor takes \(4\) seconds.
  • When the elevator stops because one or more passengers need to get off, the door stays open for \(5\) seconds plus an extra \(1\) second for each person exiting.

Initially, the elevator is at floor 0 and there are several passengers inside with known destination floors. The elevator route is planned in one trip as follows:

  1. From floor 0, visit each destination floor in increasing order (ignoring any passengers whose destination is 0 because they are already there). If multiple passengers share a destination floor, the door is only opened once at that floor with the extra time calculated from the total number of passengers exiting.
  2. After stopping at all the requested floors, the elevator must return directly to floor 0. No door opening is needed at floor 0 when returning.

Formally, let the stops (destination floors greater than 0) be sorted in increasing order. For each stop:

  Time += (current_floor_difference) * 6      \(\text{ (upward movement time) }\)
  Time += 5 + (number_of_passengers_exiting_at_this_floor)   \(\text{ (door open time) }\)

Finally, add the time for the downward journey from the last stop to floor 0:

  Time += (last_stop_floor) * 4     \(\text{ (downward movement time) }\)

Your task is to compute the total time needed to complete the trip given the initial passengers' destination floors.

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 105), representing the number of passengers initially in the elevator.

The second line contains n space-separated integers, each representing a destination floor for a passenger. Note that a destination floor of 0 means the passenger is already at floor 0, and no action is required for that passenger.

outputFormat

Output a single integer representing the total time (in seconds) required to complete the entire elevator trip as described.

sample

3
2 2 5
63