Elevator
Time limit1sMemory limit1024 MB
Rusnė goes from floor 0 to floor N using the elevator for at most K segments; minimize the total height of the segments she climbs on foot.
- Level
Easy3 of 10
- Topics
- Greedy, Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
Rusnė lives on floor of a building and uses the building's elevator every day. One day, for the sake of her health, she decides she will ride the elevator for at most floor-segments. However, the segments between consecutive floors have different heights (measured by the number of stairs), and Rusnė is clever: she wants to climb as few stairs on foot as possible.
You are given the height of every segment in the building. Find the minimum number of stairs Rusnė must climb on foot if she may ride the elevator for at most segments. (Rusnė may enter and exit the elevator as many times as she likes.)
Input
The first line contains two integers and — the floor Rusnė lives on, and the maximum number of segments she will travel by elevator.
Each of the next lines contains one integer — the height, in stairs, of the segment between floor and floor (for from to ).
The floors of the building are numbered from to . Floor is at ground level, and the elevator can be called on every floor.
Output
Print one integer — the minimum number of stairs Rusnė must climb on foot to get home.