Namje Adventure
Time limit3sMemory limit512 MB
N people hang at depths 1 to N and must reach the bottom depths D-N+1 to D; only the highest person can move down 1 to L steps, find the least total energy.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Math
- Solved
- No attempts yet
Problem
A sinkhole opened in the middle of Namjegwan. The 1333 family measured its depth from the time a stone took to hit the bottom, and they now want to explore it. They borrow a rope that Jooheon kept for a hobby and start climbing down.


Figure 1. A descent that is allowed (left) and a descent that is not allowed (right).
With a rope of length , a person can move down by any distance from to . Moving down by costs energy. The rope is too short for safe joint descent, so only the highest person moves each time. A person cannot move into a depth that another person occupies.
Depth is the distance from the hole entrance. At the start, the people hang at depths , one per depth. At the bottom, the people must occupy depths , one per depth. It does not matter who stands where.
Find the minimum total energy spent until everyone reaches the bottom.
Input
The first line contains , and . The second line contains separated by spaces.
All values are integers with , , and for every .
Output
Print the minimum total energy spent until the family reaches the bottom.
Hint
Figure 1 shows the movement rule. A descent is allowed when the destination depth is empty, and it is blocked when another person occupies it.
The figure below shows two arrivals. The left side uses the minimum energy and the right side arrives with more energy.

